CMake有多个子目录未编译

CMake with multiple subdirectories not compiling

本文关键字:编译 子目录 CMake      更新时间:2023-10-16

我的问题是CMake拒绝编译给出以下错误:

 [orpheus@Poseidon build]$ make 
 sources : /home/orpheus/Projects/Personal/C++/test/testProj/src/helloworld/MyClass.cpp/home/orpheus/Projects/Personal/C++/test/testProj/src/helloworld.cpp
 headers : /home/orpheus/Projects/Personal/C++/test/testProj/include/helloworld/MyClass.hpp
 -- Configuring done
 -- Generating done
 -- Build files have been written to: /home/orpheus/Projects/Personal/C++/test/build
 [ 33%] Building CXX object CMakeFiles/TEST.dir/testProj/src/helloworld/MyClass.cpp.o
 /home/orpheus/Projects/Personal/C++/test/testProj/src/helloworld/MyClass.cpp:1:51: fatal error: testProj/include/helloworld/MyClass.hpp: No such file or directory
  #include "testProj/include/helloworld/MyClass.hpp"
                                                    ^
 compilation terminated.
 make[2]: *** [CMakeFiles/TEST.dir/build.make:63:      CMakeFiles/TEST.dir/testProj/src/helloworld/MyClass.cpp.o] Error 1
 make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/TEST.dir/all] Error 2
 make: *** [Makefile:84: all] Error 2

我对CMake相对较新,并且已经通过了我可以找到的大多数教程以及我可以找到的所有相关的堆栈溢出问题,但是似乎没有什么能够满足我的特殊需求,我确实(在我的调整中)偶然发现了一种没有发生这种情况的方式(不幸的是我现在无法复制),但是这给了我一个链接错误声明:

 c++; fatal error: no input files

我应该使用include_directory()命令而不是set()来添加头文件吗?

我决定不使用按CMake文档添加源的glob()方法,并基于我的解决方案的这个线程和各种教程。

我有以下的项目结构:

test/
 |
 +--CMakeLists.txt
 +--build/
 |     |----CMakeFiles/
 |     |----etc
 |
 +--testProj/
 |     +----CMakeLists.txt
 |     +----include
 |     |       +----CMakeLists.txt
 |     |       +----helloworld/
 |     |       |        +----CMakeLists.txt
 |     |       |        +----MyClass.hpp  
 |     |
 |     |----src
 |     |     +----helloworld.cpp
 |     |     +----CMakeLists.txt
 |     |     +----helloworld/
 |     |     |       +----CMakeLists.txt
 |     |     |       +----MyClass.cpp

文件如下:

测试/CMakeFiles.txt

 cmake_minimum_required(VERSION 3.6.1)
 project(TEST)
 add_subdirectory(testProj)
 add_executable(${CMAKE_PROJECT_NAME} ${sources} ${headers})
 set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 set(extraFlags "-Wall -Wextra -Weffc++ -pedantic -pedantic-errors -std=c++14")
 set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${extraFlags})

测试/testProj/CMakeLists.txt

 include_directories(include)
 add_subdirectory(src)
 add_subdirectory(include)
 set(sources ${sources} PARENT_SCOPE)
 set(headers ${headers} PARENT_SCOPE)

测试/testProj/include/CMakeLists.txt

 include_directories(helloworld)
 add_subdirectory(helloworld)
 set(headers ${headers} PARENT_SCOPE)

测试/testProj/include/helloworld/CMakeLists.txt

 set(
    headers
    ${headers}
    ${CMAKE_CURRENT_SOURCE_DIR}/MyClass.hpp
    PARENT_SCOPE
)

测试/testProj/src/CMakeLists.txt

 add_subdirectory(helloworld)
 set(
        sources 
        ${sources} 
        ${CMAKE_CURRENT_SOURCE_DIR}/helloworld.cpp 
        PARENT_SCOPE
 )

测试/testProj/src/helloworld/CMakeLists.txt

 set(
    sources
    ${sources}
    ${CMAKE_CURRENT_SOURCE_DIR}/MyClass.cpp
    PARENT_SCOPE
    )

谁能给我指个方向,我要把头从监视器里探出来了

这确实是我的'include_directories()'这是问题,我没有意识到这是一个递归命令,使工作在包含子目录冗余,我通过添加以下行来修复这个问题到我的根CMakeLists.txt

 target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/testProj/include/)

,然后修改我的#include语句,以反映testProj/include目录现在在我的包含路径。

以及删除嵌套子目录中其他CMakeList文件中的冗余代码

感谢Jacob Panikulum为我指明了正确的方向。

Try

#include <MyClass.hpp>

引号告诉编译器查找一个相对路径。相对于MyClass.cpp

,标题确实不存在于该路径中

尖括号告诉编译器开始查找CMake include_directories文件夹。由于您执行了include_directories(hello_world),当您包含<*>.

时,编译器将开始查找include/hello_world