MickaëlG
MickaëlG

Reputation: 1437

cmake transitive private include directory

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 ?

Upvotes: 2

Views: 1544

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54679

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.

Upvotes: 2

Related Questions