[CMake] [Q] howto merge include dirs from dependencies?

Michael Wild themiwi at gmail.com
Wed Sep 1 08:01:08 EDT 2010


On 1. Sep, 2010, at 13:47 , Paweł Sikora wrote:

> Dnia 01-09-2010 o 13:08:59 Michael Wild <themiwi at gmail.com> napisał(a):
> 
>> 
>> On 1. Sep, 2010, at 12:36 , Paweł Sikora wrote:
>> 
>>> hi,
>>> 
>>> i'm thinking about migration my custom makefiles for very large
>>> project to cmake system and generally have a one problem.
>>> 
>>> e.g. i have a library (mylib) and a binary (coreApp) placed
>>> in subdirectiories with one top-level cmakelists.txt:
>>> 
>>> 
>>> top-level dir:
>>> CMakeLists.txt { cmake_minimum_required (VERSION 2.6) add_subdirectory (core) add_subdirectory (mylib) }
>>> core
>>> mylib
>>> 
>>> ./core subdir:
>>> CMakeLists.txt { add_executable (coreApp main.cpp) target_link_libraries (coreApp mylib) }
>>> main.cpp
>>> 
>>> ./mylib subdir:
>>> CMakeLists.txt { include_directories (h) add_library (mylib SHARED mylib.cpp) }
>>> h
>>> mylib.cpp
>>> 
>>> ./mylib/h:
>>> mylib.hpp
>>> 
>>> for mylib build i'm adding the "h" subdir with interface headers
>>> but i need the mylib/h include path also for coreApp build.
>>> is there a simply way to pull include dirs from dependencies?
>> 
>> Put include_directories(mylib/h) into the top-level CMakeLists.txt.
> 
> naturally but include_directories() in top-level cmakelists.txt is quite
> hard to maintance for live project with few levels of hierarchy and hundreds
> of shared components. implicit include-dirs pulling from dependencies
> to current build target would be great.
> i wondering is it possible to do it with some kind of properties?


It's gonna be more code ;-) And you have to be take care in which order you call add_subdirectory.

* toplevel/CMakeLists.txt:
add_subdirectory(mylib) # MUST be before core
add_subdirectory(core)

* toplevel/mylib/CMakeLists.txt:
set(MYLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/h) # MUST be absolute paths!
set_property(GLOBAL PROPERTY MYLIB_INCLUDE_DIRS ${MYLIB_INCLUDE_DIRS})
include_directories(${MYLIB_INCLUDE_DIRS})
add_library(mylib SHARED mylib.cpp)

* toplevel/core/CMakeLists.txt:
get_property(HAVE_MYLIB_INCLUDE_DIRS GLOBAL PROPERTY MYLIB_INCLUDE_DIRS DEFINED)
if(NOT HAVE_MYLIB_INCLUDE_DIRS)
  message(FATAL_ERROR "MYLIB_INCLUDE_DIRS property not defined, "
    "probably due to wrong add_subdirectory ordering.")
endif()
get_property(MYLIB_INCLUDE_DIRS GLOBAL PROPERTY MYLIB_INCLUDE_DIRS)
include_directories(${MYLIB_INCLUDE_DIRS})
add_executable(coreApp main.cpp)
target_link_libraries(coreApp mylib)

If you want to, you can wrap the whole property stuff in some easy to use convenience functions.

HTH

Michael


More information about the CMake mailing list