C++ CMake 找不到包含文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15449949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMake can not find include files
提问by Amani
I have a project with the following layout:
我有一个具有以下布局的项目:
/build
/source
+--- CMakeLists.txt
|
+--- /bin
| +--CMakefiles.txt
| +--main.cpp
|
+--- /jsoncpp
| +--- /json
| | +--json.h
| | +--json-forwards.h
| |
| +--jsoncpp.cpp
| +--CMakeLists.txt
|
+--- /jsonreader
+-- jsonreader.cpp
+-- jsonreader.h
+-- CMakeLists.txt
In /source/CMakeLists.txt i have this line of code;
在 /source/CMakeLists.txt 我有这行代码;
include_directories(jsoncpp jsonreader)
but then running 'cmake -G "MSYS Makefiles" ../source' in build directory generates Makefile and then running 'make' generates the following error:
但是然后在构建目录中运行 'cmake -G "MSYS Makefiles" ../source' 生成 Makefile,然后运行 'make' 生成以下错误:
Scanning dependencies of target updater
[ 33%] Building CXX object bin/CMakeFiles/updater.dir/main.cpp.obj
In file included from k:/own-projects/updater-Project/withJsonCpp/source/bin/main.cpp:2:0:
../source/jsonreader/jsonreader.h:2:18: fatal error: json.h: No such file
or directory
compilation terminated.
make[2]: *** [bin/CMakeFiles/updater.dir/main.cpp.obj] Error 1
make[1]: *** [bin/CMakeFiles/updater.dir/all] Error 2
make: *** [all] Error 2
what am i doing wrong and how can i solve this?
我做错了什么,我该如何解决?
回答by drescherjm
There were two problems. Firstly you have to add the jsoncpp/jsonpath to your included directories. However, doing so creates a second problem. Since your executables are not in the source folder you needed to prefix ${CMAKE_SOURCE_DIR}to your paths so include_directories()would look like following:
有两个问题。首先,您必须将jsoncpp/json路径添加到包含的目录中。然而,这样做会产生第二个问题。由于您的可执行文件不在源文件夹中,因此您需要为${CMAKE_SOURCE_DIR}路径添加前缀,因此include_directories()如下所示:
include_directories("${CMAKE_SOURCE_DIR}/jsoncpp"
"${CMAKE_SOURCE_DIR}/jsoncpp/json"
"${CMAKE_SOURCE_DIR}/jsonreader")
I've added quotes just out of habit. I do this most of the time with my CMakeLists.txtso there are no problems with spaces in paths.
我只是出于习惯添加了引号。我大部分时间都是用我的,CMakeLists.txt所以路径中的空格没有问题。
回答by Eran
Amani,
阿曼尼,
It seems as if you include "json.h" without its relative path. You can either include it like this:
似乎您包含了没有其相对路径的“json.h”。您可以像这样包含它:
#include "json/json.h"
OR, in your CMakeLists.txt file, add the json directory to the include directories:
或者,在您的 CMakeLists.txt 文件中,将 json 目录添加到包含目录:
include_directories(jsoncpp jsoncpp/json jsonreader)

