0

I have a project that looks like this.

. ├──include | └──utilities.hpp ├──source | ├──main.cpp | └──utilities.cpp └──CMakeLists.txt 

Where both source/main.cpp and source/utilities.cpp have an #include "include/utilities.hpp directive, and the contents of CMakeLists.txtare:

cmake_minimum_required(VERSION 3.16.3) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) project(search VERSION 1.0.0) add_executable( search source/utilities.cpp source/main.cpp include/utilities.hpp ) include_directories(search PUBLIC include) 

When I run make VERBOSE+1, I get the following output:

[ 33%] Building CXX object CMakeFiles/search.dir/source/utilities.cpp.o /home/mohammed/Work/Projects/search/source/utilities.cpp:1:10: fatal error: include/utilities.hpp: No such file or directory 1 | #include "include/utilities.hpp" | ^~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. make[2]: *** [CMakeFiles/search.dir/build.make:63 : CMakeFiles/search.dir/source/utilities.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:76 : CMakeFiles/search.dir/all] Error 2 make: *** [Makefile:84 : all] Error 2 

I tried all the answers to this question. But none of them worked.

Can anyone tell me what am I doing wrong?

p.s If it is not yet clear, I am new to CMake.

1
  • In CMake, add_executable only needs the .cpp files. You've added include/utilities.hpp. This causes confusion between the compiler and CMake where to find that file. (Not an answer, since the quoted error message is caused by an earlier problem compiling utilities.cpp) Commented Aug 3, 2021 at 14:15

3 Answers 3

3

include_directories != target_include_directories. Looks like you meant to use the latter.

Additionally, as RoQuOTriX pointed out: When you add a directory to the include path, includes should be relative to that directory. So if include is on your include path, you would want to #include "utilities.hpp" instead of #include "include/utilities.hpp".

Sign up to request clarification or add additional context in comments.

1 Comment

I tried both include_directories(include) and target_include_directories(searchPUBLIC include) with similar results.
2

If you specify include_directories() directly you don't need to use the relative paths in your source files:

#include "include/utilities.hpp" 

Instead use:

#include "utilities.hpp" 

If you instead want to keep the relative paths then you need to remove the include_directories() from the CMakeLists.txt.

The include_directories() tells the compiler where it should search for includes.

1 Comment

That actually worked. I don't know how I managed to miss the fact that the verbose message was saying -I ./include rather than -I . Thanks a lot.
1

I solved the problem by changing the contents of CMakeLists.txt to:

cmake_minimum_required(VERSION 3.16.3) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) project(search VERSION 1.0.0) set(WORKING_DIRECTORY /home/mohammed/Work/Projects/search) add_executable( search ${WORKING_DIRECTORY}/source/utilities.cpp ${WORKING_DIRECTORY}/source/main.cpp ${WORKING_DIRECTORY}/include/utilities.hpp ) include_directories(${WORKING_DIRECTORY}) 

A huge thanks to RoQuOTrix.

1 Comment

It is not necessary to add .hpp files to your add_executable statement, and you should really use target_include_directories over include_directories (in addition to removing include/ from your #include statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.