我的项目中有两个实用程序库。我正在寻找为库编写CMake配置的最佳方法。
/my_project
--> CMakeLists.txt
--> main.cpp
--> /utils
--> CMakeLists.txt
--> common.h
--> /base_c
--> CMakeLists.txt
--> base_c.c
--> base_c.h
--> /base_cpp
--> CMakeLists.txt
--> base_cpp.cpp
--> base_cpp.hpp
我当前的CMake文件:
/my_project/CMakeLists.txt
cmake_minimum_requared(VERSION 3.8)
project(my_project)
add_subdirectory(utils)
add_executable(main main.c)
target_link_libraries(main utils base_c base_cpp)
/my_project/utils/CMakeLists.txt
add_subdirectory(base_c)
add_subdirectory(base_cpp)
add_library(utils)
target_sources(utils PUBLIC common.h)
/my_project/utils/base_c/CMakeLists.txt
add_library(base_c base_c.c)
target_sources(base_c PUBLIC base_c.h)
/my_project/utils/base_cpp/CMakeLists.txt
add_library(base_cpp base_cpp.cpp)
target_sources(base_cpp PUBLIC base_cpp.hpp)
find_library(BASEC base_c ../base_c)
target_link_libraries(base_cpp BASEC)
问题在于
base_cpp
找不到base_c
中的包含内容。我应该如何修复配置?我设法使它与
target_include_directories(base_cpp PRIVATE ../base_c)
一起使用,但这很丑陋,根据INTERFACE_INCLUDE_DIRECTORIES documentation,它不是必需的。 最佳答案
正如@Anedar提到的,要解决这种情况,需要在target_include_directories
库中使用带有PUBLIC
或INTERFACE
选项的CMakeLists.txt
。这将填充库目标的INTERFACE_INCLUDE_DIRECTORIES
,供消费方的target_link_libraries
使用。
我的工作配置:
/my_project/CMakeLists.txt
cmake_minimum_requared(VERSION 3.8)
project(my_project)
add_subdirectory(utils)
add_executable(main main.c)
target_link_libraries(main utils base_c base_cpp)
/my_project/utils/CMakeLists.txt
add_subdirectory(base_c)
add_subdirectory(base_cpp)
add_library(utils)
target_sources(utils PUBLIC common.h)
target_include_directories(utils INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
/my_project/utils/base_c/CMakeLists.txt
add_library(base_c base_c.c)
target_sources(base_c PUBLIC base_c.h)
target_include_directories(base_c INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
/my_project/utils/base_cpp/CMakeLists.txt
add_library(base_cpp base_cpp.cpp)
target_sources(base_cpp PUBLIC base_cpp.hpp)
target_include_directories(base_cpp INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(base_cpp base_c)
关于c++ - 在CMake中将标题包含在库中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46296487/