我有一个具有以下目录结构的 CMake 项目:
- CMakeLists.txt
- A/CMakeLists.txt
- B/CMakeLists.txt
A 和 B 都描述共享库,并且 B 依赖于 A。
在整个项目上调用cmake
不会出现任何问题。构建A也没有,但构建B告诉我它错过了A中的符号。这是有道理的,因此我通过将 target_link_libraries(B A)
添加到 B/CMakeLists.txt 来解决这个问题。
但是,当我现在调用 cmake
时,我收到有关 B/CMakeLists.txt 无法从 A 找到源文件的错误。显然,为了解决这个问题,我应该将 target_include_directories(A PUBLIC .)
添加到 A/CMakeLists.txt 中,但这不起作用。我在这里缺少什么?
为了完整起见,这里是简化的 CMake 文件:
cmake_minimum_required (VERSION 3.5.1)
project(main C CXX)
add_subdirectory(A)
add_subdirectory(B)
A/CMakeLists.txt
cmake_minimum_required (VERSION 3.5.1)
add_library(A SHARED "")
target_include_directories(A PUBLIC .)
target_sources(A PUBLIC ...)
B/CMakeLists.txt
cmake_minimum_required (VERSION 3.5.1)
add_library(B SHARED "")
target_link_libraries(B A)
target_sources(B PUBLIC ...)
我得到的错误如下
CMake Error at B/CMakeLists.txt:3 (add_library): Cannot find source file:
Access.hpp
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
我正在关注this tutorial ,解释 target_link_libraries
和 target_include_directories
。
最佳答案
不要使用target_sources()
。将源文件添加到 add_library()
命令。
或者将target_sources()
与PRIVATE
一起使用。
关于CMake:在同级库的 add_library 上找不到源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422056/