6
votes

I'm developing a C++ project in Linux using CMake.

I am creating two libraries, LibA and LibB. I do not want LibA and LibB to have the same include directories. Is there any way I can set only LibA to include DirectoryA and set only LibB to include DirectoryB?

Edit:

Both LibA and LibB will be used in an executable, MyExe. When I #include LibA.h and LibB.h in MyExe's source code, I cannot have the included header files from DirectoryA and DirectoryBcoexisting in MyExe, as this will create namespace conflicts.

Is this possible?

Edit 2 : Here is my CMakeLists.txt include_directories(include)

add_library(LibA src/LibA.cpp include/LibA.h)
set_property(TARGET LibA PROPERTY INCLUDE_DIRECTORIES /opt/SomeLibrary2.0/include/)
target_link_libraries(LibA /opt/SomeLibrary2.0/lib/a.so /opt/SomeLibrary2.0/lib/b.so /opt/SomeLibrary2.0/lib/c.so)

add_library(LibB src/LibB.cpp include/LibB.h)
set_property(TARGET LibB PROPERTY INCLUDE_DIRECTORIES ${LIB_B_INCLUDE_DIRS})
target_link_libraries(LibB ${LIB_B_LIBRARIES})

add_executable(MyExe src/myexe.cpp)
target_link_libraries(MyExe LibA LibB)

But I'm still getting errors. LibA.h says that SomeLibrary's header files cannot be found?

1
Split the CMake files, make them subdirectories. - IdeaHat
Thank you for your comment. My original question was updated to better reflect my issue. - trianta2
I cannot have the included header files from DirectoryA and DirectoryB coexisting in MyExe, as this will create namespace conflicts IMHO it is design issue, if you solve include conflicts you may have strange linker errors and unexpected runtime behaviour - user2288008

1 Answers

13
votes

If you can specify CMake version 2.8.12 as the minimum, you can use target_include_directories. This was introduced in version 2.8.11, but I think it was a bit buggy until 2.8.12.

So you can do:

target_include_directories(LibA PRIVATE DirectoryA)
target_include_directories(LibB PRIVATE DirectoryB)

If you have to support older versions of CMake, you can set the INCLUDE_DIRECTORIES property on the targets appropriately:

set_property(TARGET LibA
             PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/DirectoryA)
set_property(TARGET LibB
             PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/DirectoryB)