我正在学习如何编写“现代” cmake配置,并试图创建将库链接到可执行文件。我的测试项目结构如下:
.
|-- foo
| |-- src
| | |-- foo.cpp
| | `-- foo_impl.cpp
| |-- include
| | `-- foo
| | `-- foo.h
| |-- internal
| | `-- foo_impl.h
| `-- CMakeLists.txt
|-- src
| `-- main.cpp
`-- CMakeLists.txt
./ CMakeLists.txt是
cmake_minimum_required(VERSION 3.12)
project(test_cmake LANGUAGES CXX)
add_subdirectory(foo)
add_executable(test_executable
src/main.cpp
)
target_include_directories(test_executable
PRIVATE
include
foo
)
./ foo / CMakeLists.txt是
cmake_minimum_required(VERSION 3.12)
add_library(foo
src/foo_impl.cpp
src/foo.cpp
)
target_include_directories(foo
PUBLIC
include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
此配置的原因是测试cmake是否将foo / internal文件夹公开给库foo的客户端。
我的配置有误吗?
答案 0 :(得分:2)
您的cmake中有类型/错误。
在CMakeLists.txt中,您可以执行以下操作:
target_include_directories(test_executable
这将添加包含目录。您需要链接到foo
项目:
target_link_libraries(test_executable
PRIVATE foo)