CMake can not find include files

2019-03-18 03:14发布

问题:

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;

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:

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?

回答1:

There were two problems. First you needed to add the path jsoncpp/json to the your include directories. However that pointed to a second problem. Since your executables not in the source folder you needed to prefix ${CMAKE_SOURCE_DIR} to your paths so the include_directories() should look like the following:

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

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



回答2:

Amani,

It seems as if you include "json.h" without its relative path. You can either include it like this:

#include "json/json.h"

OR, in your CMakeLists.txt file, add the json directory to the include directories:

include_directories(jsoncpp jsoncpp/json jsonreader)