我正在使用CMake在
Linux中开发一个C项目.
我正在创建两个库,LibA和LibB.我不希望LibA和LibB拥有相同的包含目录.有什么办法我只能设置LibA来包含DirectoryA并且只设置LibB来包含DirectoryB吗?
编辑:
LibA和LibB都将用于可执行文件MyExe.当我在MyExe的源代码中#include LibA.h和LibB.h时,我无法在MyExe中拥有DirectoryA和DirectoryBcoexisting中包含的头文件,因为这会产生命名空间冲突.
这可能吗?
编辑2:这是我的CMakeLists.txt
include_directories(包括)
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)
解决方法
如果您可以指定CMake版本2.8.12作为最低版本,则可以使用
target_include_directories
.这是在2.8.11版本中引入的,但我认为它有点儿错误,直到2.8.12.
所以你可以这样做:
target_include_directories(LibA PRIVATE DirectoryA) target_include_directories(LibB PRIVATE DirectoryB)
如果必须支持较旧版本的CMake,则可以在目标上正确设置INCLUDE_DIRECTORIES
属性:
set_property(TARGET LibA PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/DirectoryA) set_property(TARGET LibB PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/DirectoryB)