Ama*_*ani 20 c++ makefile cmake
我有一个具有以下布局的项目:
/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
Run Code Online (Sandbox Code Playgroud)
在/source/CMakeLists.txt我有这行代码;
include_directories(jsoncpp jsonreader)
Run Code Online (Sandbox Code Playgroud)
但是然后在构建目录中运行'cmake -G"MSYS Makefile"../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
Run Code Online (Sandbox Code Playgroud)
我做错了什么,怎么解决这个问题?
dre*_*rjm 25
有两个问题.首先,您需要添加jsoncpp/json包含目录的路径.然而,这指出了第二个问题.由于您的可执行文件不在源文件夹中,因此您需要为${CMAKE_SOURCE_DIR}路径添加前缀,因此include_directories()应如下所示:
include_directories("${CMAKE_SOURCE_DIR}/jsoncpp"
"${CMAKE_SOURCE_DIR}/jsoncpp/json"
"${CMAKE_SOURCE_DIR}/jsonreader")
Run Code Online (Sandbox Code Playgroud)
我只是出于习惯而添加引号.我大部分时间都这样做,CMakeLists.txt所以路径中的空格没有问题.
阿曼尼,
看起来好像你没有它的相对路径包含"json.h".您可以像这样包含它:
#include "json/json.h"
Run Code Online (Sandbox Code Playgroud)
或者,在您的CMakeLists.txt文件中,将json目录添加到include目录:
include_directories(jsoncpp jsoncpp/json jsonreader)
Run Code Online (Sandbox Code Playgroud)