[CMake] Help on first cmake project

Florian Lindner mailinglists at xgm.de
Sun Jul 9 05:45:05 EDT 2017



Am 07.07.2017 um 16:47 schrieb J Decker:
> 
> 
> On Thu, Jul 6, 2017 at 11:45 PM, Florian Lindner <mailinglists at xgm.de <mailto:mailinglists at xgm.de>> 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





More information about the CMake mailing list