Help on first cmake project

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help on first cmake project

Florian Lindner
Hello,

coming from scons I want to take a look into cmake...

My CMakeList.txt looks like:


cmake_minimum_required (VERSION 3.0)
project (ASTE)

add_executable(readMesh readMesh.cpp)

find_library(precice precice PATHS $ENV{PRECICE_ROOT}/build/last)
target_link_libraries(readMesh ${precice})
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} $ENV{PRECICE_ROOT}/build/last)


find_package(Boost 1.60.0
  COMPONENTS program_options filesystem
  REQUIRED)
target_link_libraries(readMesh ${Boost_LIBRARIES})

find_package(MPI REQUIRED)
target_link_libraries(readMesh ${MPI_LIBRARIES})
set(COMPILE_FLAGS  ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(LINK_FLAGS ${LINK_FLAGS} ${MPI_LINK_FLAGS})


and does it job, i.e. the target builds. precice is out own, independently build library.

However, I have several questions:

* Can I make target_link_libraries add a library for all defined targets? At the moment, there is only one, but in the
projekt there are multiple, very similiar targets built.

* Can I make find_library to treat the library as required, as with find_package?

* Can I reused the value of find_library's PATH setting? Especially if there are multiple PATHs, wouldn't it be nice to
have a precice-LIBPATH that contains the path, where the library was found? So I can add this to CMAKE_LIBRARY_PATH? Or
is there another function that does all that?

* Is there a way to clean cmake cache, other than rm -rf CMakeFiles CMakeCache.txt. Would be nice, when playing around
and frequently changing the CMakeLists.txt

* I am a bit suprised that finding/or not finding MPI or precice is not printed out when executing cmake.

* Any other advises you want to give me?

Thanks for helping a beginner!

Florian

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help on first cmake project

J Decker


On Thu, Jul 6, 2017 at 11:45 PM, Florian Lindner <[hidden email]> wrote:
Hello,

coming from scons I want to take a look into cmake...

My CMakeList.txt looks like:


cmake_minimum_required (VERSION 3.0)
project (ASTE)

add_executable(readMesh readMesh.cpp)

find_library(precice precice PATHS $ENV{PRECICE_ROOT}/build/last)
target_link_libraries(readMesh ${precice})
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} $ENV{PRECICE_ROOT}/build/last)


find_package(Boost 1.60.0
  COMPONENTS program_options filesystem
  REQUIRED)
target_link_libraries(readMesh ${Boost_LIBRARIES})

find_package(MPI REQUIRED)
target_link_libraries(readMesh ${MPI_LIBRARIES})
set(COMPILE_FLAGS  ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(LINK_FLAGS ${LINK_FLAGS} ${MPI_LINK_FLAGS})


and does it job, i.e. the target builds. precice is out own, independently build library.

However, I have several questions:

* Can I make target_link_libraries add a library for all defined targets? At the moment, there is only one, but in the
projekt there are multiple, very similiar targets built.
No; each target needs to have target_link_libaries.  You can build a list of libs to add like set( common_deps ${Boost_LIBRARIES} ${MPI_LIBRARIES} ) ; or appending to a variable like you did with COMPILE_FLAGS.


* Can I make find_library to treat the library as required, as with find_package?
 
can add if( <VAR>-NOTFOUND ) message( ERROR "xxx not found" )
can wrap that in a macro for simplification...

macro( my_find_library var )
  find_library( ${var} ${ARGN} )
  if( ${var}-NOTFOUND )
    message( FATAL_ERROR "${ARG2} was not found" )
  endif()
endmacro()

(assumes that you're using the single name version, and not multiple names using NAME command in find_library)

* Can I reused the value of find_library's PATH setting? Especially if there are multiple PATHs, wouldn't it be nice to
have a precice-LIBPATH that contains the path, where the library was found? So I can add this to CMAKE_LIBRARY_PATH? Or
is there another function that does all that?

Yes, these become available for all sub projects also.  INCLUDE_DIRECTORIES() and LINK_DIRECTORIES() will also get inherited by child projects.
 
* Is there a way to clean cmake cache, other than rm -rf CMakeFiles CMakeCache.txt. Would be nice, when playing around
and frequently changing the CMakeLists.txt

really just remove CMakeCache.txt is sufficient; though generally you won't have to remove the cache; it redoes the generation if one of the cmakelists.txt is updated (or included dependancies).  If you're moving librarites to be found, then yes you will have to remove the cmakecache.
 
* I am a bit suprised that finding/or not finding MPI or precice is not printed out when executing cmake.

* Any other advises you want to give me?

Thanks for helping a beginner!

Florian

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help on first cmake project

Gonzalo Garramuño
In reply to this post by Florian Lindner


El 07/07/17 a las 03:45, Florian Lindner escribió:
> * Any other advises you want to give me?
I would advise you try to build out of source tree to keep the source
clean.  Example:

$ mkdir build-linux64
$ cd build-linux64
$ cmake .. -G'Unix Makefiles'
$ make
$ make install

when done, you can then do:

$ cd ..
$ rm -rf build-linux64

> Thanks for helping a beginner!
>
> Florian
No problem.

--
Gonzalo Garramuño

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help on first cmake project

Florian Lindner
In reply to this post by J Decker


Am 07.07.2017 um 16:47 schrieb J Decker:

>
>
> On Thu, Jul 6, 2017 at 11:45 PM, Florian Lindner <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Hello,
>
>     coming from scons I want to take a look into cmake...
>
>     My CMakeList.txt looks like:
>
>
>     cmake_minimum_required (VERSION 3.0)
>     project (ASTE)
>
>     add_executable(readMesh readMesh.cpp)
>
>     find_library(precice precice PATHS $ENV{PRECICE_ROOT}/build/last)
>     target_link_libraries(readMesh ${precice})
>     set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} $ENV{PRECICE_ROOT}/build/last)
>
>
>     find_package(Boost 1.60.0
>       COMPONENTS program_options filesystem
>       REQUIRED)
>     target_link_libraries(readMesh ${Boost_LIBRARIES})
>
>     find_package(MPI REQUIRED)
>     target_link_libraries(readMesh ${MPI_LIBRARIES})
>     set(COMPILE_FLAGS  ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
>     set(LINK_FLAGS ${LINK_FLAGS} ${MPI_LINK_FLAGS})
>
>
>     and does it job, i.e. the target builds. precice is out own, independently build library.
>
>     However, I have several questions:
>
>     * Can I make target_link_libraries add a library for all defined targets? At the moment, there is only one, but in the
>     projekt there are multiple, very similiar targets built.
>
> No; each target needs to have target_link_libaries.  You can build a list of libs to add like set( common_deps
> ${Boost_LIBRARIES} ${MPI_LIBRARIES} ) ; or appending to a variable like you did with COMPILE_FLAGS.

Ok, that would be nice feature I think.

>     * Can I make find_library to treat the library as required, as with find_package?
>
>  
> can add if( <VAR>-NOTFOUND ) message( ERROR "xxx not found" )

Ok. I am a bit surprised, that there is no REQUIRED flag, just like it is with the similar find_package.

> can wrap that in a macro for simplification...
>
> macro( my_find_library var )
>   find_library( ${var} ${ARGN} )
>   if( ${var}-NOTFOUND )
>     message( FATAL_ERROR "${ARG2} was not found" )
>   endif()
> endmacro()

find_library(precice precice PATHS $ENV{PRECICE_ROOT}/build/last)
if( precice-NOTFOUND )
  message(FATAL_ERROR "preCICE was not found")
endif()

but the missing library is not caught and the subsequent call to target_link_libraries fails.

Using if(NOT precice) instead made it working.

> (assumes that you're using the single name version, and not multiple names using NAME command in find_library)
>
>
>     * Can I reused the value of find_library's PATH setting? Especially if there are multiple PATHs, wouldn't it be nice to
>     have a precice-LIBPATH that contains the path, where the library was found? So I can add this to CMAKE_LIBRARY_PATH? Or
>     is there another function that does all that?
>
> Yes, these become available for all sub projects also.  INCLUDE_DIRECTORIES() and LINK_DIRECTORIES() will also get
> inherited by child projects.

Not sure if I got that, but we'll see...

Best,
Florian



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Loading...