CMakelists.txt;未找到嵌套文件夹头文件错误
我有以下文件夹结构:CMakelists.txt;未找到嵌套文件夹头文件错误,c,cmake,cmakelists-options,C,Cmake,Cmakelists Options,我有以下文件夹结构: demo_main | |---- demo_use | | | --- demo_use_main.c ---- demo_1.0 | --- demo_sample | ---zmq_sample | --- HelloWorld.h
demo_main
|
|---- demo_use
| |
| --- demo_use_main.c
---- demo_1.0
|
--- demo_sample
|
---zmq_sample
|
--- HelloWorld.h
--- HelloWorld.cpp
在demo\u main\demo\u use\demo\u use\u main.c文件中,我需要调用位于另一个文件夹中的HelloWorld.cpp中定义的函数
demo_use_main.c
#include "HelloWorld.h"
...
int main(int argc, char *argv[])
{
...
HelloWorldPrint();
return 0;
}
HelloWorld.h
void HelloWorldPrint(void);
HelloWorld.cpp
#include<iostream>
#include "HelloWorld.h"
void HelloWorldPrint()
{
std::cout << "function Hello World!" << std::endl;
}
我的Cmakelists.txt看起来像:
demo_main\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
add_subdirectory(demo_use)
add_subdirectory(demo_1.0)
demo_main\demo_use\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../demo_1.0/demo_sample/zmq_sample/)
add_sources(
${CMAKE_CURRENT_LIST_DIR}/demo_use_main.c
...
)
demo_main\demo_1.0\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
add_subdirectory(demo_sample)
demo_main\demo_1.0\demo_sample\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
add_subdirectory(zmq_sample)
demo_main\demo_1.0\demo_sample\zmq_sample\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
project (zmq_demo)
add_sources(${CMAKE_CURRENT_LIST_DIR}/HelloWorld.cpp)
add_executable(${PROJECT_NAME} HelloWorld.cpp)
demo\u use\u main.c找不到头文件缺少什么?
如何更正?如注释所述,未找到标题,因为
include\u directories
调用仅适用于当前目录(demo\u main\demo\u use
)或子目录中定义的目标。您在当前目录中没有定义目标,因此include目录实际上没有应用于任何对象
CMake代码几乎没有其他问题
main
函数是在demo\u use\u main.c
中定义的,因此您的add\u executable
调用应该包括该文件2.8.12
更新的版本。从软件工程的角度来看,现代CMake中的命令更直观,应该更易于使用project
的调用。通常,将其包括在内是一种良好的做法,您可以通过稍后再次调用project
来在其下添加后续子项目。此外,您只需要在顶级cmake文件中调用一次所需的cmake\u minimum\u
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
# Add top-level project call.
project(DemoProject)
# Add the sub-directories more directly.
add_subdirectory(demo_use)
add_subdirectory(demo_1.0\demo_sample\zmq_sample)
project(zmq_demo)
# Add the executable here.
add_executable(zmq_demo demo_use_main.c)
# Add the HelloWorld.cpp source file to the executable's compilation.
target_sources(zmq_demo PRIVATE HelloWorld.cpp)
# Prefer target-based command here to include directories, if CMake version supports it.
target_include_directories(zmq_demo PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
demo\u main\demo\u use\CMakeLists.txt:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
# Add top-level project call.
project(DemoProject)
# Add the sub-directories more directly.
add_subdirectory(demo_use)
add_subdirectory(demo_1.0\demo_sample\zmq_sample)
project(zmq_demo)
# Add the executable here.
add_executable(zmq_demo demo_use_main.c)
# Add the HelloWorld.cpp source file to the executable's compilation.
target_sources(zmq_demo PRIVATE HelloWorld.cpp)
# Prefer target-based command here to include directories, if CMake version supports it.
target_include_directories(zmq_demo PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
demo\u main\demo\u 1.0\demo\u sample\zmq\u sample\CMakeLists.txt:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
# Add top-level project call.
project(DemoProject)
# Add the sub-directories more directly.
add_subdirectory(demo_use)
add_subdirectory(demo_1.0\demo_sample\zmq_sample)
project(zmq_demo)
# Add the executable here.
add_executable(zmq_demo demo_use_main.c)
# Add the HelloWorld.cpp source file to the executable's compilation.
target_sources(zmq_demo PRIVATE HelloWorld.cpp)
# Prefer target-based command here to include directories, if CMake version supports it.
target_include_directories(zmq_demo PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
我建议花一些时间了解使用的好处,因为从长远来看,它可能会使维护更容易。
2.8.12版已经8年了,您有机会安装更新的cmake吗?是的,这是我添加的宏。但它没有任何作用。我将把它从问题中删除。无法使用较新版本作为其客户机代码。那么添加源代码做什么?include_目录
仅影响当前目录及以下目录中定义的目标。在demo\u main\demo\u use
中执行include\u目录
,不会影响外部任何内容。(现在,使用target\u include\u directories
)add\u sources是wrapper over set“call”set(${PROJECT\u NAME}\u sources…)设置一些${PROJECT\u NAME}\u sources
变量有什么意义?非常感谢您的建议和示例。我在cmakelist中尝试了上述更改,但问题仍然存在。