Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to add an imported library to target_link_libraries that takes care of include directories too?

Tags:

c++

include

cmake

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

like image 753
Martin Avatar asked Sep 18 '14 08:09

Martin


People also ask

What does Cmake Target_link_libraries do?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

How do I link libraries from one target to another?

Libraries for both a Target and its Dependents ¶. target_link_libraries (<target> <item>...) Library dependencies are transitive by default with this signature. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too.

How do I create a named Library target?

The named target must be created by add_library () within the project or as an IMPORTED library. If it is created within the project an ordering dependency will automatically be added in the build system to make sure the named library target is up-to-date before the <target> links.

What does <target_link_libraries> mean?

target_link_libraries (<target> <item>...) Library dependencies are transitive by default with this signature. When this target is linked into another target then the libraries linked to this target will appear on the link line for the other target too. This transitive “link interface” is stored in the INTERFACE_LINK_LIBRARIES target property ...

Why are object files placed on the link line before libraries?

Such object files are placed on the link line before all libraries, regardless of their relative order. Additionally, an ordering dependency will be added to the build system to make sure the object library is up-to-date before the dependent target links. For example, the code


Video Answer


1 Answers

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/v3.0/manual/cmake-buildsystem.7.html#transitive-usage-requirements

like image 86
steveire Avatar answered Oct 12 '22 22:10

steveire