Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake can not find include files

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?

like image 901
Amani Avatar asked Mar 16 '13 13:03

Amani


People also ask

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is Cmake_source_dir?

The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .


2 Answers

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") 

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.

like image 179
drescherjm Avatar answered Oct 04 '22 18:10

drescherjm


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) 
like image 22
Eran Avatar answered Oct 04 '22 19:10

Eran