CMake:在兄弟库的add_library上找不到源文件

时间:2016-07-17 14:03:09

标签: cmake

我有一个具有以下目录结构的CMake项目:

  • 的CMakeLists.txt
  • A /的CMakeLists.txt
  • B /的CMakeLists.txt

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 ...)

我得到的错误是以下

  

B / CMakeLists.txt的CMake错误:3(add_library):     找不到源文件:

     

Access.hpp

     

尝试扩展.c .C .c ++ .cc .cpp .cxx .m .M .mm .h .hh .h ++ .hm .hpp .hxx .in .txx

我正在关注this tutorial,解释有关target_link_librariestarget_include_directories的信息。

1 个答案:

答案 0 :(得分:3)

请勿使用target_sources()。将源文件添加到add_library()命令。

或将target_sources()PRIVATE一起使用。