[CMake] Help on first cmake project

J Decker d3ck0r at gmail.com
Fri Jul 7 04:47:05 EDT 2017


On Thu, Jul 6, 2017 at 11:45 PM, Florian Lindner <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.


> * 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170707/205620ad/attachment.html>


More information about the CMake mailing list