具有多个子目录的 CMake 未编译
CMake with multiple subdirectories not compiling
我的问题是 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()
添加头文件吗?
我决定不使用 glob()
根据 CMake documentation and have based my solution off of this thread 和各种教程添加源的方法。
我有以下项目结构:
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
文件如下:
test/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})
test/testProj/CMakeLists.txt
include_directories(include)
add_subdirectory(src)
add_subdirectory(include)
set(sources ${sources} PARENT_SCOPE)
set(headers ${headers} PARENT_SCOPE)
test/testProj/include/CMakeLists.txt
include_directories(helloworld)
add_subdirectory(helloworld)
set(headers ${headers} PARENT_SCOPE)
test/testProj/include/helloworld/CMakeLists.txt
set(
headers
${headers}
${CMAKE_CURRENT_SOURCE_DIR}/MyClass.hpp
PARENT_SCOPE
)
test/testProj/src/CMakeLists.txt
add_subdirectory(helloworld)
set(
sources
${sources}
${CMAKE_CURRENT_SOURCE_DIR}/helloworld.cpp
PARENT_SCOPE
)
test/testProj/src/helloworld/CMakeLists.txt
set(
sources
${sources}
${CMAKE_CURRENT_SOURCE_DIR}/MyClass.cpp
PARENT_SCOPE
)
请有人指出正确的方向,因为我正要通过显示器。
尝试
#include <MyClass.hpp>
引号告诉编译器寻找相对路径。 header 在相对于 MyClass.cpp
的那个路径上确实不存在
angle-brackets 告诉编译器开始查找您的 CMake include_directories
文件夹。由于您执行了 include_directories(hello_world)
,当您包含 <*>.
时,编译器将开始查找 include/hello_world
这确实是我的 'include_directories()' 问题,我没有意识到这是一个 recursive command 使 include 子目录中的工作变得多余,我通过将以下行添加到我的根 CMakeLists.txt
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/testProj/include/)
然后修改我的 #include
语句以反映 testProj/include
目录现在在我的包含路径中。
以及删除嵌套子目录中其他 CMakeList 文件现在的冗余代码
感谢 Jacob Panikulum 为我指明了正确的方向。
我的问题是 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()
添加头文件吗?
我决定不使用 glob()
根据 CMake documentation and have based my solution off of this thread 和各种教程添加源的方法。
我有以下项目结构:
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
文件如下:
test/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})
test/testProj/CMakeLists.txt
include_directories(include)
add_subdirectory(src)
add_subdirectory(include)
set(sources ${sources} PARENT_SCOPE)
set(headers ${headers} PARENT_SCOPE)
test/testProj/include/CMakeLists.txt
include_directories(helloworld)
add_subdirectory(helloworld)
set(headers ${headers} PARENT_SCOPE)
test/testProj/include/helloworld/CMakeLists.txt
set(
headers
${headers}
${CMAKE_CURRENT_SOURCE_DIR}/MyClass.hpp
PARENT_SCOPE
)
test/testProj/src/CMakeLists.txt
add_subdirectory(helloworld)
set(
sources
${sources}
${CMAKE_CURRENT_SOURCE_DIR}/helloworld.cpp
PARENT_SCOPE
)
test/testProj/src/helloworld/CMakeLists.txt
set(
sources
${sources}
${CMAKE_CURRENT_SOURCE_DIR}/MyClass.cpp
PARENT_SCOPE
)
请有人指出正确的方向,因为我正要通过显示器。
尝试
#include <MyClass.hpp>
引号告诉编译器寻找相对路径。 header 在相对于 MyClass.cpp
angle-brackets 告诉编译器开始查找您的 CMake include_directories
文件夹。由于您执行了 include_directories(hello_world)
,当您包含 <*>.
include/hello_world
这确实是我的 'include_directories()' 问题,我没有意识到这是一个 recursive command 使 include 子目录中的工作变得多余,我通过将以下行添加到我的根 CMakeLists.txt
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/testProj/include/)
然后修改我的 #include
语句以反映 testProj/include
目录现在在我的包含路径中。
以及删除嵌套子目录中其他 CMakeList 文件现在的冗余代码
感谢 Jacob Panikulum 为我指明了正确的方向。