cmake: add directory to be searched for header files - c++

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)

Related

CMake + ninja - exe file shows no output if header file is included in a source file

I am learning to use CMake and I'm using ninja as a build system. I have created a very simple program, but I have one issue. I have only two files main.cpp and constants.h. Whenever I include constants.h in main.cpp, after building a .exe file, I see no output from it after running it through bash. When i remove the include, it works just fine. Can anybody please guide.
main.cpp
#include <stdio.h>
#include "constants.h" // does not even show output when i add this
int main() {
printf("hello world");
return 0;
}
constants.h
#pragma once
const int WIDTH = 1000;
CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
set(CMAKE_CXX_COMIPLER "g++")
set(CMAKE_C_COMPILER "gcc")
project(BoidSimulation)
set(CMAKE_CXX_STANDARD 17)
add_executable(BoidSimulation main.cpp)
Can someone please guide what I'm doing wrong. I have tried to add constants.h in add_exectuable and also used target_include_directories by keeping the header file in some directory.
I build and run through the following commands:
cmake -G "Ninja" -S . -B ./out/build/
cd out/build
ninja
./BoidSimulation
Running this with header file shows no output and no errors on bash, it just moves on to next line. It runs fine through powershell and shows the output with or without header files.
You never print a newline. In some shells this will cause the prompt to be printed over it. This has nothing to do with your constants.h header.

Problem with CMake finding my header file

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

Clear way to setup LSP (vim-lsc) with vim + CMake?

I am trying to set up LSP with vim + cmake. I am using vim-lsc plugin as client and clangd as the server.
Folder structure:
Test/
test.cpp
test.h
CMakeLists.txt
test.h:
#ifndef TEST_H
#define TEST_H
class Apple {
public:
int size;
};
#endif
test.cpp:
#include <iostream>
#include "test.h"
int main() {
Apple apple;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(my_test)
add_executable(test test.cpp)
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
~/.vimrc:
let g:lsc_server_commands = {'c': 'clangd', 'cpp': 'clangd'}
I build with
mkdir build
cd build
cmake ..
make
This compiles fine with no errors and warnings.
However, while editing test.cpp in vim, vim-lsc shows errors not detecting user defined header files and types defined within them. For example, in the above project, the following two errors are shown:
test.cpp|3 col 10 error| 'test.h' file not found [pp_file_not_found]
test.cpp|6 col 5 error| Unknown type name 'Apple' [unknown_typename]
How do I set this up so that headers in the project are identified properly?
EDIT: The problem has changed somewhat. I added a compile_commands.json file to the top level directory to no avail. I suspect that it has to do with the way paths are represented in Git Bash on windows.
The compile_commands.json has the inlcude paths like so:
-IE:\\C:\\Test
If the class is defined in the same file, the lsp protocol seems to be working. Can clang not work with such paths? Could someone shed some light on this?

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?

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)