Problem with CMake finding my header file - c++

So I am trying to compile a small project that I am doing of a Sokoban Solver. For that, I have the following folder structure:
The CMakeLists file that I have generated is quite simple and contains the following lines of code:
project(Sokoban)
cmake_minimum_required(VERSION 3.16.3)
include_directories(include/)
add_executable(main src/main.cpp src/coord.cpp src/map.cpp src/node_data.cpp src/node.cpp src/search_methods.cpp)
As I understand it:
Executable needs to have the *.cpp files
The directory with the *.hpp files must be included somehow to the CMake so the compiler knows where to look for them.
When I try to compile, I obtain the following error:
[build] ../src/main.cpp:4:10: fatal error: include/search_methods.hpp: No such file or directory
[build] 4 | #include "include/search_methods.hpp"
[build] | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] compilation terminated.
Does anyone know how or which other information would you need to solve this issue? I have been looking into similar problems, but did not solve the issue.

As #tkausl pointed out
Either drop the include folder from your #include statements or from your include path; can't have both.
Change #include "include/search_methods.hpp" to #include "search_methods.hpp" and it should work.
Also consider to use a more modern CMake approach using
target_include_directories(main
PUBLIC
include
)
and git rid of include_directories(include/)

Related

How to include files in a subdirectory of a CMake project, without using another CMakeLists.txt

I have a project added as a submodule to another project using CMake. The submodule is just two files (b.c, b.h) so the project structure is:
root/
| CMakeLists.txt
| main.cc
| a.cc
| a.h
| submodule/
| | b.c
| | b.h
I don't want to add another CMakeLists.txt to the submodule as it's just another git thing to maintain. The submodule will never grow beyond these two files so I just want something simple and CMake is overkill for some of my other projects, so I don't want it in the submodule's git repo.
If I try include_directories(submodule) and add submodule/b.c to the sources list of my add_executable block, I get undefined references to the functions inside b.c/h
Is there a quick and simple way to just add these extra files to the sources without getting undefined references? Or is there a better way of managing this tiny library?
I have
include_directories(${PROJECT_SOURCE_DIR}/submodule)
...
add_executable(${NAME}
submodule/b.c
main.cpp
server.cc
)
I have also tried adding
target_link_directories(${NAME} PUBLIC
${PROJECT_SOURCE_DIR}/submodule
)
The error is:
/usr/lib/gcc/arm-none-eabi/10.3.1/../../../arm-none-eabi/bin/ld: CMakeFiles/light.dir/
server.cc.obj: in function `tcpRecvCallback(void*, tcp_pcb*, pbuf*, signed char) [clon
e .part.0]':
server.cc:(.text._Z15tcpRecvCallbackPvP7tcp_pcbP4pbufa.part.0+0x62): undefined referen
ce to `http_req_parse(char*)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/light.dir/build.make:3083: light.elf] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:1520: CMakeFiles/light.dir/all] Error 2
gmake: *** [Makefile:156: all] Error 2
where server.cc is in the project root, and http_req_parse is a function in b.c
main.cpp includes server.h which includes submodule/b.h
It may be relevant that I am building for a raspberry pi pico W.
new answer:
include_directories(${PROJECT_SOURCE_DIR}/submodule)
set(SOURCES submodule/b.c)
add_executable(${PROJECT_NAME} SOURCES)
old answer:
include_directories(${PROJECT_SOURCE_DIR}/submodule)
file(GLOB submodule_source submodule/*.c)
add_executable(${PROJECT_NAME} submodule_source)
It turns out that all the above answers were correct, I was forgot to use the language specific guards, since my library was in C, and my project was in C++.
IE, I didn't have
#ifdef _cplusplus
extern "C" {
#endif

cmake: add directory to be searched for header files

I am writing c++ code to run tests on an arduino project I've written. In order to run it, I need it to include mockups of a couple libraries (the original libraries don't work in c++). The mockups are in my src directory, along with the tests in main.cpp. The files that rely on the mockups are in the parent directory of src, and I cannot modify them to refer to src without breaking their ability to run when uploaded to an arduino. I also cannot add the mockups or main.cpp to the parent directory, as this would interfere with the operation of the arduino code.
So, I need to add the child directory with the mockups to the directories that are searched when compiling the files in the parent directory.
Directory Outline:
parent
forTest.h
forTest.cpp
src
parson.h
parson.c
String.h
String.cpp
main.cpp
CMakeLists.txt
build
In this case, main.cpp has "#include ../forTest.cpp", and forTest.cpp has "#include parson.h" and "#include String.h"
I am currently running the following code in CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project(makeTest)
include_directories(../ )
set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)
add_executable(makeTest main.cpp ../forTest.cpp String.cpp parson.c)
I then build it in the build directory from the command line with
cmake -G "Unix Makefiles" ../src
make
The cmake command resolves successfully, but the make command hits "fatal error: parson.h not found" when building forTest.cpp
How can I resolve this?
edit: my apologies if this has an obvious answer, I'm very new to Cmake
edit the second: using the changes suggested by Gergely Nyiri and changing #include to #include "/usr/include/string.h" resolves several errors, but introduces a new error : "implicit instantiation of undefined template" during the make step.
It refers to the following code in by string.h mockup file:
#ifndef STRING_H
#define STRING_H
#include "/usr/include/string.h"
using namespace std;
class String {
private:
std::string thisString;
char chars[];
...
#endif
which returns the error:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:61:
/Users/douglas/Desktop/parts/makeHolder/testMake/src/string.h:9:14: error:
implicit instantiation of undefined template 'std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >'
std::string thisString;
This is followed by:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:193:32: note:
template is declared here
class _LIBCPP_TEMPLATE_VIS basic_string;
First of all, I would propose to put CMakeLists.txt in your source root. Then:
cmake_minimum_required (VERSION 2.6)
project(makeTest)
include_directories(src)
set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)
add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
Configure & build:
cd build
cmake ../
make
Even better if you use target_include_directories instead of include_directories:
..
add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
target_include_directories(makeTest PRIVATE src)

include_directories do not include directories

Referring the original project as it is easier to explain.
I'm trying to compile this project:
https://github.com/jbohg/render_kinect
I get the error
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp:57:43: fatal
error: render_kinect/kinectSimulator.h: No such file or directory
#include <render_kinect/kinectSimulator.h>
^
When clearly kinectSimulator.h is present in the include directory. And
include_directories(${PROJECT_SOURCE_DIR}/include)
present in the cmake.
I changed #include <render_kinect/kinectSimulator.h> to #include "render_kinect/kinectSimulator.h".
Added include_directories(${PROJECT_SOURCE_DIR}/include/renderer_kinect) to cmake followed by #include "kinectSimulator.h" in the source
Nothing seems to work. Looks like include_directories function is not affecting at all. Though the paths show up in the
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message("inc_dirs = ${inc_dirs}")
gives
inc_dirs = /home/sarkar/libs/render_kinect/include;other/include/paths
Why is this still not working?
EDIT:
make verbose do not show -I option.
make VERBOSE=1
/usr/bin/c++ <options> -o CMakeFiles/RenderKinect.dir/src/kinectSimulator.o -c
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp:57:43: fatal
error: render_kinect/kinectSimulator.h: No such file or directory
Are the INCLUDE_DIRECTORIES properties completely being ignored?

Make C Project Instead of C++

I am having trouble setting up a C projectin Clion. I changed the name of the main.cpp to main.c, and altered the CMakeLists file accordingly, with the following info:
cmake_minimum_required(VERSION 3.3)
project(Project_1__)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.c)
add_executable(Project_1__ ${SOURCE_FILES})
However I get the following error when I try to build:
fatal error: iostream: No such file or directory #include compilation terminated. mingw32-make.exe[3]: * [CMakeFiles/Project_1__.dir/main.c.obj] Error 1 mingw32-make.exe[2]:
* [CMakeFiles/Project_1__.dir/all] Error 2
This error is due to fact that your complier is unable to find iostream header file. Make sure it exists in that directory. Plus it also depends on your IDE.
Also make sure, it is declared as #include "iostream.h" instead of #include<iostream.h>
iostream header files are declared in many of the compilers is declared by #include<iostream>
C++ needs <iostream> header file while C needs only <stdio.h>
Replace #include <iostream> with #include <stdio.h>. You'll also have to replace the stream related stuff in your code with stdio functions. See man stdio.
Also, you may have to change your makefile as I see -std=c++11 and I suspect that should be -std=c11. I'm not completely clear on this, so YMMV
iostream.h is specific to c++, c do not use stream classes

Using Cuda Object Linking with Cmake

Right now I am working on a project that uses the object linking capabilities of Cuda 5. Since the project is starting to get complex, I wanted to switch to using cmake to compile the code. However I can't seem to get object linking to work correctly for me.
I ended up creating a toy version of the project which gets the same kind of errors as the original project. The toy project consists of a main file (TextureMain.cu) that calls a kernel function to run on the GPU. In each GPU thread an instance of a user-defined class (TextureFunc) is referenced, where the class exists in a separate folder from the main file. The class consists of a TextureFunc.cu and TextureFunc.h file in that folder.
Here are the CMakeList.txt files I am using:
In the project directory (contains src directory):
project(TextureMain)
cmake_minimum_required(VERSION 2.8)
find_package(CUDA REQUIRED)
#-------------------------------------------------------------------------------
set(CUDA_NVCC_FLAGS "-arch=compute_20; -code=sm_20; -rdc=true; -lcudadevrt")
include_directories(src/TextureFunc)
#-------------------------------------------------------------------------------
add_subdirectory(src/TextureFunc)
add_subdirectory(src)
In the src directory (contains TextureMain.cu and TextureFunc directory):
cuda_add_executable(TextureMain TextureMain.cu)
target_link_libraries(TextureMain TextureFunc)
install(TARGETS TextureMain DESTINATION bin)
In TextureFunc directory (contains TextureFunc.h and TextureFunc.cu):
cuda_add_library(TextureFunc TextureFunc.cu )
target_link_libraries(TextureFunc)
When I try to compile this code using the above CMakeList.txt files, I get the following error.
Linking CXX executable TextureMain
CMakeFiles/TextureMain.dir/./TextureMain_generated_TextureMain.cu.o: In function `__sti____cudaRegisterAll_46_tmpxft_00004c15_00000000_6_TextureMain_cpp1_ii_texRef':
/tmp/tmpxft_00004c15_00000000-3_TextureMain.cudafe1.stub.c:2: undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_00004c15_00000000_6_TextureMain_cpp1_ii_texRef'
TextureFunc/libTextureFunc.a(TextureFunc_generated_TextureFunc.cu.o): In function `__sti____cudaRegisterAll_46_tmpxft_00004bd8_00000000_6_TextureFunc_cpp1_ii_421ca072':
/tmp/tmpxft_00004bd8_00000000-3_TextureFunc.cudafe1.stub.c:8: undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_00004bd8_00000000_6_TextureFunc_cpp1_ii_421ca072'
collect2: ld returned 1 exit status
make[2]: *** [src/TextureMain] Error 1
make[1]: *** [src/CMakeFiles/TextureMain.dir/all] Error 2
This is obviously a linking error, and it probably has to do with the way that I compile the code using cmake. I think the flags for nvcc are right since I was able to compile this project using a Makefile with the same flags. However, I'm not really sure what else I could be doing wrong. I did notice that the error message references some non-existent .cpp files, but I don't know what to do with that.
Any advice that can be given would be greatly appreciated. I'm using cmake version 2.8.8.
How about linking${CUDA_LIBRARIES} to whatever targets using `.cu files?
For example in your src directory you could try:
cuda_add_executable(TextureMain TextureMain.cu)
target_link_libraries(TextureMain TextureFunc ${CUDA_LIBRARIES})
install(TARGETS TextureMain DESTINATION bin)