CMake找不到包含文件 [英] CMake can not find include files

查看:362
本文介绍了CMake找不到包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下布局的项目:

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

在/source/CMakeLists.txt中,我有这行代码;

In /source/CMakeLists.txt i have this line of code;

include_directories(jsoncpp jsonreader)

但是在构建目录中运行'cmake -G"MSYS Makefiles" ../source会生成Makefile,然后运行'make'会产生以下错误:

but then running 'cmake -G "MSYS Makefiles" ../source' in build directory generates Makefile and then running 'make' generates the following error:

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?

推荐答案

有两个问题.首先,您必须将jsoncpp/json路径添加到包含的目录中.但是,这样做会产生第二个问题.由于您的可执行文件不在源文件夹中,因此您需要在路径前添加${CMAKE_SOURCE_DIR}前缀,因此include_directories()如下所示:

There were two problems. Firstly you have to add the jsoncpp/json path 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:

include_directories("${CMAKE_SOURCE_DIR}/jsoncpp"
    "${CMAKE_SOURCE_DIR}/jsoncpp/json"
    "${CMAKE_SOURCE_DIR}/jsonreader")

我只是出于习惯添加了引号.我大部分时间都是通过CMakeLists.txt进行此操作的,因此路径中的空格没有问题.

I've added quotes just out of habit. I do this most of the time with my CMakeLists.txt so there are no problems with spaces in paths.

这篇关于CMake找不到包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆