Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Different Include Directories For Different Targets?

Tags:

c++

linux

cmake

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?

like image 887
trianta2 Avatar asked Dec 17 '13 19:12

trianta2


1 Answers

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)
like image 148
Fraser Avatar answered Sep 19 '22 21:09

Fraser