23

somehow I am struggling with finding out whether it is possible to define an imported library in CMake, specifying target properties (include_directories and library path) and hoping that CMake will append the include directories once I add that project to target_link_libraries in another project.

Let's say I have an imported library in a file called Module-Conf.cmake:

add_library(mymodule STATIC IMPORTED)
set_target_properties(mymodule PROPERTIES IMPORTED_LOCATION "${OUTPUT_DIR}/lib")
set_target_properties(mymodule PROPERTIES INCLUDE_DIRECTORIES "${OUTPUT_DIR}/include")

And in a project I add the dependency:

include(Module-Conf)
target_link_libraries(${PROJECT_NAME} mymodule)

Will CMake append the include_directories property to the include path? Right now I cannot see the path so it seems that I have to do it by myself by using get_target_property?

Question: Can I do some CMake magic to automatically append the include to the include directories of another project?

Thanks a lot. Martin

Martin
  • 968
  • 3
  • 10
  • 19

2 Answers2

25

The difference between the INCLUDE_DIRECTORIES property and the INTERFACE_INCLUDE_DIRECTORIES property is transitivity.

Set INTERFACE_INCLUDE_DIRECTORIES instead.

http://www.cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#transitive-usage-requirements

starball
  • 20,030
  • 7
  • 43
  • 238
steveire
  • 10,694
  • 1
  • 37
  • 48
1

Starting from CMake 3.11, it's possible to use target_include_directories() with IMPORTED targets.

add_library(mymodule SHARED IMPORTED)
target_include_directories(mymodule INTERFACE
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

Another way is set_property(), which also allows using generator expressions.

set_property(TARGET mymodule PROPERTY INTERFACE_INCLUDE_DIRECTORIES
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
Andrei
  • 750
  • 8
  • 9