6

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?

trianta2
  • 3,952
  • 5
  • 36
  • 52
  • 2
    Split the CMake files, make them subdirectories. – IdeaHat Dec 17 '13 at 19:51
  • Thank you for your comment. My original question was updated to better reflect my issue. – trianta2 Dec 17 '13 at 20:07
  • 1
    `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 –  Dec 17 '13 at 21:07

1 Answers1

13

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)
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Thank you for your answer. My original question was updated to better reflect my issue. – trianta2 Dec 17 '13 at 20:08
  • I'm currently using 2.8.7, so not as of now. If 2.8.12 has features which will solve my problem then I will update CMake. – trianta2 Dec 17 '13 at 20:11
  • 1
    I updated my answer to show libs rather than exes. There should be no need to upgrade CMake - using either method should not cause the include directories of the libraries to become include dirs of the exe. – Fraser Dec 17 '13 at 20:14
  • So I unwittingly installed the one of the libraries "DirectoryA" to my /usr/local/ directory, so the entire project already sees it. I'm reinstalling the library to /opt/DirectoryA/ and giving this a shot. Rebuilding this library could be process however. – trianta2 Dec 17 '13 at 20:23
  • 1
    I think this is now a design issue. I'm guessing that `LibA` builds OK on its own, but when you try to build `MyExe` the failure happens. If `LibA.h` includes a header from `/opt/SomeLibrary2.0/include/` (directly or indirectly via one of its other include files), and `MyExe` includes `LibA.h`, then `/opt/SomeLibrary2.0/include/` *must* be available as a search path for `MyExe` too. Off the top of my head, you can maybe consider using the [pimpl idiom](http://stackoverflow.com/q/3597693/2556117) to avoid having to expose `SomeLibrary2.0`'s headers to `MyExe`. – Fraser Dec 17 '13 at 22:10