C++ 为什么在并行文件夹CMake中找不到文件

C++ 为什么在并行文件夹CMake中找不到文件,c++,linux,cmake,include-path,cmake-language,C++,Linux,Cmake,Include Path,Cmake Language,为了让我的.cpp和.h文件在履行职责之前得到一点整理,我决定将它们放在不同的文件夹中 我使用了以下结构: 根目录 | -CMakeLists.txt[rootCmakeList] src | -main.cpp ...| ....... 数学 ...| ….-CMakeLists.txt[mathCmakeList] ……代数.h ……代数.cpp ….XML[xmlCmakeList] ...| ….-CMakeLists.txt ….-AwesomeXML.h ….-AwesomeX

为了让我的.cpp和.h文件在履行职责之前得到一点整理,我决定将它们放在不同的文件夹中 我使用了以下结构:

根目录
|
-CMakeLists.txt[rootCmakeList]
src
|
-main.cpp
...|
....... 数学
...|
….-CMakeLists.txt[mathCmakeList]
……代数.h
……代数.cpp
….XML[xmlCmakeList]
...|
….-CMakeLists.txt
….-AwesomeXML.h
….-AwesomeXML.cpp

[rootCmakeList]看起来:

cmake_minimum_required(VERSION 3.11.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_DEBUG  "-g")

project(myProject)
#add exectuable
add_executable(myProject src/main.cpp)

target_include_directories(myProject PUBLIC "${src/XML}") 
target_include_directories(myProject PUBLIC "${src/math}") 

add_subdirectory(src/XML) 
add_subdirectory(src/math) 

target_link_libraries(myProject xml)#needed? 
target_link_libraries(myProject math)#needed? 
[mathCmakeList]看起来:

include_directories(${myProject}src/math)
add_library(math Algebra.cpp) 
include_directories(${myProject}src/xml) 
add_library(xml AwesomeXML.cpp) 
[xmlCmakeList]看起来:

include_directories(${myProject}src/math)
add_library(math Algebra.cpp) 
include_directories(${myProject}src/xml) 
add_library(xml AwesomeXML.cpp) 
到目前为止还不错,没有问题。但是如果我想把代数.h包括进去 AweseomeXML.cpp我找不到该文件


老实说,我甚至不确定cmake命令add_library和target_link_libraries在这里是否真的有意义,因为我不想创建自己的库,只是想整理一点我的文件,这些文件的主题待定。

myProject
似乎不是您设置的变量。此外,如果正确设置库目标,则不需要手动将任何include目录添加到myProject

首先为
math
设置
CMakeLists.txt
文件,因为它不依赖于其他项目。我建议将链接库使用的标题移动到子目录。我自己通常使用
include
,您希望链接库在
#include
中使用的任何路径都从那里开始。设置include dir的方式是将它们添加到
接口\u include\u目录
数学
目标属性

add_library(math Algebra.cpp
    include/Algebra.h # for IDEs
)

target_include_directories(math # should be static here, since you don't want to deploy the lib by itself
    PUBLIC include # available to both math and linking libs
    PRIVATE .      # only available to math; only necessary, if there are "private" headers
)
然后对
xml
执行同样的操作,但是由于您使用的是
math
中的功能,因此需要将其链接起来,以便自动访问其包含目录,因为它们可以通过
接口\u include\u目录使用:

add_library(xml STATIC
    AwesomeXML.cpp
    include/AwesomeXML.h
)

target_include_directories(xml PUBLIC include)

target_link_libraries(xml PRIVATE math) # change PRIVATE to PUBLIC, if Algebra.h is included in a public header
现在在顶层,我们应该确保在定义目标之前,目标的任何依赖项都是可用的。链接库还可以访问其各自的公共包含目录和公共包含目录,这些目录是公开链接的依赖项:

cmake_minimum_required(VERSION 3.11.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_DEBUG  "-g")

project(myProject)

add_subdirectory(src/math)
add_subdirectory(src/XML) 

#add exectuable
add_executable(myProject src/main.cpp)

target_link_libraries(myProject PRIVATE math xml)

变量取消引用“${src/XML}”
没有意义。您可能希望使用
“src/XML”
。这也是关于
“${src/math}”
@Tsyvarev的,你确定吗?这似乎没有改变什么。但是现在在main.cpp中甚至找不到这些文件。我确信
“${src/XML}”
是错误的。我确信
${myProject}
也是错误的。“老实说,我甚至不确定cmake命令
add_library
target_link_libraries
在这里是否有意义,因为我不想创建自己的库”-如果你不想要库,那么为什么要创建它们?只需将所有源文件添加到
add_executable
call中即可。