2
votes

Given this cmake sample project:

lib1/CMakeLists.txt:

add_library(lib1 src1.cpp)
target_include_directories(lib1 PUBLIC include)

lib2/CMakeLists.txt:

add_library(lib2 src2.cpp)
target_include_directories(lib2 PUBLIC include)
target_link_libraries(lib2 PRIVATE lib1)

lib3/CMakeLists.txt:

add_library(lib3 src3.cpp)
target_include_directories(lib3 PUBLIC include)
target_link_libraries(lib3 PRIVATE lib2)

All .cpp and .h files are dummy files with no dependency between them.

From what I understood from the documentation, adding a "PRIVATE" target library from lib2 to lib1 means that lib1 include directory should not be added when compiling lib3. However, when launching compiling this using cmake (3.3.2) and "make VERBOSE=1", command line to compile "src3.cpp" contains "-I/.../lib1/include":

c++ -I/.../lib3/include -I/.../lib2/include -I/.../lib1/include -o .../src3.cpp.o -c /.../lib3/src3.cpp

What did I get wrong ?

1
Hi, could you try with, $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> in target_include_library Because I don't have any include errors when precising, the build and install interface. - Noki
Changing "target_include_directories(lib1 PUBLIC include)" to "target_include_directories(lib1 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)" doesn't change the result. I don't have any error. I would like to have lib1 not included while linking lib3. - MickaëlG
What is your cmake_minimum_required version? This old thread on the CMake mailing list suggests that the problem might disappear if you use cmake_minimum_required(VERSION 3.0) or higher. - ComicSansMS
You are right. I had "2.8". When changed to "3.0" it works as expected. Thanks ! - MickaëlG

1 Answers

2
votes

From what I understood from the documentation, adding a "PRIVATE" target library from lib2 to lib1 means that lib1 include directory should not be added when compiling lib3.

Your assumptions are correct.

However, due to legacy reasons in how properties propagate between dependent targets, this correct behavior is deactivated when specifying an old version in cmake_minimum_required.

By changing the version to 3.0 or later, you will get the correct behavior:

cmake_minimum_required(VERSION 3.0)

 ...

See also this thread on the CMake mailing list.