CLion can't find CMake generated headers? - c++

Going through the CMake tutorial:
├── CMakeLists.txt
├── src
│   └── main.cpp
└── templates
└── fooConf.h.in
CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
project(foo)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PROJECT_SOURCE_DIR src)
set(PROJECT_TEMPLATE_DIR templates)
set(SOURCE_FILES ${PROJECT_SOURCE_DIR}/main.cpp)
set(${PROJECT_NAME}_MAJOR 0)
set(${PROJECT_NAME}_MINOR 1)
set(${PROJECT_NAME}_MICRO 1)
configure_file (
"${PROJECT_TEMPLATE_DIR}/fooConf.h.in"
"${PROJECT_SOURCE_DIR}/fooConf.h"
)
add_executable(foo ${SOURCE_FILES})
templates/fooConf.h.in
#define #PROJECT_NAME#_VERSION_MAJOR #PROJECT_NAME##_VERSION_MAJOR#
#define #PROJECT_NAME#_VERSION_MINOR #PROJECT_NAME##_VERSION_MINOR#
#define #PROJECT_NAME#_VERSION_MICRO #PROJECT_NAME##_VERSION_MICRO#
src/main.cpp
#include <iostream>
#include "src/fooConf.h"
// Also tried: "fooConf.h"
int main() {
std::cout << foo_VERSION_MAJOR;
return 0;
}
[Error]
fatal error: src/fooConf.h: No such file or directory

Your fooConf.h header file is generated in the binary tree (precisely, under ${CMAKE_BINARY_DIR}/src/). So you should issue corresponded include_directories() command for use that file in #include. E.g.:
CMakeLists.txt
....
include_directories(${CMAKE_BINARY_DIR})
add_executable(foo ${SOURCE_FILES})
src/main.cpp
....
#include "src/fooConf.h"

Related

CMake header files can't be found (fatal error: file not found)

I'm working on a CMake project but Im getting this error and I don't understand why. Im using VS Code on MacOS and I'm using Clang as the compiler.
Here is the structure of my folder project:
tree
.
├─ app
│ ├── CMakeLists.txt
│ └── src
│ └── main.cpp
├─ CMakeLists.txt
├─ build
└─ engine
├── CMakeLists.txt
├── external
├── include
│ └── project_engine
│ └── engine.h
└── src
└── engine.cpp
The root CMakeLists.txt is
cmake_minimum_required(VERSION 3.5)
project(vulkanEngine_developement_env VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_subdirectory(app)
add_subdirectory(engine)
The CMakeLists.txt in the app folder:
cmake_minimum_required(VERSION 3.5)
project(vulkanEngine VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} vulkanEngine_engine)
target_include_directories(${PROJECT_NAME} PRIVATE vulkanEngine_engine)
and the one in engine is:
cmake_minimum_required(VERSION 3.5)
project(vulkanEngine_engine VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_library(${PROJECT_NAME} src/engine.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
PRIVATE
)
Whenever I try to include the header file in both cpp files I get the error "file not found", I tried using both
#include <project_engine/engine.h>
and
#include "project_engine/engine.h"
I also tried linking the path to the header in the vs code include path setting but it didn't work, I added include_directories to the engine CMake file but that didn't work either.

How do I configure a program built with CMake to read and write files from it's bin folder?

I'm making a game engine in C++ with CMake, I have the following directory structure:
Engine/
│ CMakeLists.txt
|
├─ lib/
| libengine.a
|
├─ example/
│ │ CMakeLists.txt
| |
│ ├─ bin/
| | example
| |
│ └─ source/
│ └─ Example.cxx
|
├─ include/
│ Engine.hxx
|
└─ source/
Engine.cxx
I've used this directory structure in a few other projects and it was ok until I encountered a problem, when I write to a file through the program it creates it in the 'Engine' directory, but I want it to create the file in 'Engine/example/bin/' (where the executable is) without any modifications to the C++ code. The same goes for reading files. At first I though this was because Engine::SaveGame() was in the engine library, so I tried to create a file through fstream in main but I got the same result.
Here are the cmake build scripts and the C++ source:
Engine/CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project("Engine" LANGUAGES CXX)
message(STATUS "Running CMakeLists.txt for Engine")
option(LIB_BUILD_EXAMPLE "Build the example" ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-m64 -s -O3 -Wall -Wextra")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY lib/)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY lib/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY lib/)
add_library(engine STATIC
source/Engine.cxx
)
target_include_directories(engine PUBLIC
include/
)
if(LIB_BUILD_EXAMPLE)
add_subdirectory(example)
endif()
Engine/include/Engine.hxx:
#pragma once
namespace Engine {
extern void SaveGame();
} // namespace Engine
Engine/source/Engine.cxx:
#include "Engine.hxx"
#include <iostream>
#include <fstream>
namespace Engine {
void SaveGame() {
printf("Saving game...\n");
std::ofstream output_file("game1.sav");
output_file << "game data";
}
} // namespace Engine
Engine/example/CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project("Example" LANGUAGES CXX)
message(STATUS "Running CMakeLists.txt for Example")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-m64 -s -Wall -Wextra")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY bin/)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY bin/)
add_executable(example
source/Example.cxx
)
target_link_libraries(example
engine
)
Engine/example/Example.cxx:
#include "Engine.hxx"
#include <fstream>
int main() {
Engine::SaveGame();
std::ofstream output_file("test.txt");
output_file << "line 1\nline 2\n";
return 0;
}
The way I build this project is by running cmake . and then make in the 'Engine' directory, and I run the example to test the engine by doing ./example/bin/example.
I think this has to do with some CMake option or something.

cmake subdirectory preprocesser include error

.
├── CMakeLists.txt
├── main.cpp
├── lib
├── CMakeLists.txt
├── Scene.cpp
└── Scene.hpp
./CMakeLists.txt is like this.
cmake_minimum_required(VERSION 3.22)
project(myapp)
find_package(OpenCV REQUIRED)
find_package(blend2d REQUIRED)
find_package(Box2D REQUIRED)
add_subdirectory(lib)
include_directories(lib)
include_directories(${OpenCV_INCLUDE_DIR})
include_directories(${Blend2D_INCLUDE_DIRS})
include_directories(${Box2D_INCLUDE_DIR})
include_directories(${CMAKE_SOURCE_DIR})
link_directories(lib)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PRIVATE Box2D)
target_link_libraries(${PROJECT_NAME} PRIVATE blend2d)
target_link_libraries(${PROJECT_NAME} PRIVATE SCENE)
./lib/CMakeLists.txt is like this
include_directories(${OpenCV_INCLUDE_DIR})
add_library(SCENE Scene.cpp Scene.hpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBRARIES})
The problem is in Scene.cpp, Scene.hpp
Scene.cpp is like this.
#include "Scene.hpp"
class Scene
{
...
}
the "#include <Scene.hpp>" makes an error that "#include errors detected based on information provided by the configurationProvider setting. Squiggles are disabled for this translation unit."
And Scene.hpp is like this.
#include <opencv2/opencv.hpp>
class Scene
{
....
}
the "#include <opencv2/opencv.hpp>" makes an error that "cannot open opencv2/opencv.hpp"
When I ignore the error and run 'cmake --build .',
It produces
[25%] Linking CXX executable myapp
/usr/bin/ld: canoot find -lSCENE
I'm using wsl ubuntu with vscode.
How can I solve it?

'torch/torch.h' file not found in header file

I'm new in Linux C++ programming. I have to use libtorch (pytorch C++ version), but I kept struggling with C++ and cmake problems. These problems really make me mad, and I need some help.
Here is a simple C++ project:
.
├── build.sh
├── CMakeLists.txt
├── load_data
│   ├── CMakeLists.txt
│   ├── include
│   │   └── load_data
│   │   └── load_data.hpp
│   └── src
│   └── load_data.cpp
└── src
└── main.cpp
But I can't build it because load_data/include/load_data/load_data.hpp cannot #include <torch/torch.h>.
The build error message:
[build] /my/path/load_data/include/load_data/load_data.hpp:4:10: fatal error: 'torch/torch.h' file not found
[build] #include <torch/torch.h>
[build] ^~~~~~~~~~~~~~~
However, if I move the #include <torch/torch.h> from the header file to the corresponding source file, the error message goes away. It makes me confused.
Files' contents:
load_data/include/load_data/load_data.hpp
#pragma once
#include <torch/torch.h>
namespace load_data
{
void try_torch();
// torch::Tensor rand_tensor();
}
load_data/src/load_data.cpp
# include "load_data/load_data.hpp"
// If I remove it from load_data.hpp and use it here, the error goes away.
// #include <torch/torch.h>
namespace load_data {
void try_torch(){
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
}
}
src/main.cpp
#include <string>
#include "load_data/load_data.hpp"
int main(int argc, char **argv)
{
load_data::try_torch();
return 0;
}
CMakeLists.txt
cmake_minimum_required (VERSION 3.21)
set(CMAKE_CXX_COMPILER clang++-13)
project(ldt-cpp-experiments VERSION 1.0.0 LANGUAGES CXX)
find_package(Torch REQUIRED)
message(STATUS "Torch_FOUND: ${Torch_FOUND}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_subdirectory("load_data")
set(EXECUTABLE_NAME main)
add_executable(${EXECUTABLE_NAME} src/main.cpp)
target_link_libraries(${EXECUTABLE_NAME}
PRIVATE load_data
)
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
)
load_data/CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project (load_data VERSION 1.0.0 LANGUAGES CXX)
find_package(Torch REQUIRED)
message(STATUS "Torch_FOUND: ${Torch_FOUND}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
set(LIB_NAME "load_data")
add_library(${LIB_NAME}
SHARED
)
target_sources(${LIB_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/src/load_data.cpp
)
target_sources(${LIB_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/src/load_data.cpp
${PROJECT_SOURCE_DIR}/include/load_data/load_data.hpp
)
target_include_directories(${LIB_NAME}
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
target_link_libraries(${LIB_NAME}
PRIVATE
${TORCH_LIBRARIES}
)
set_target_properties(${LIB_NAME} PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
)
build.sh
cmake -S . -B build

How do you add external libraries to 'self-made' libraries using CMake?

I'm not able to link external libraries with the libraries I wrote using CMake. I'm wondering if there's something that is needed to be added to my CMakeLists.txt? Or if I need to add another CMakeLists.txt in a lower level (inside src) and what would that need to contain?
I have the following project structure:
ProjectFolder
│   ├── CMakeLists.txt
│   ├── build
│   │   └──
│   ├── include
│   │   └── helper.h
│   └── src
│   ├── helper.cpp
   └── main.cpp
My CMakeList.txt is:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(object_detection)
find_package(PCL 1.5 REQUIRED)
find_package(OpenCV REQUIRED)
file(GLOB SOURCES src/*.cpp)
file(GLOB INCLUDE include/*.h)
include_directories(${PCL_INCLUDE_DIRS} ${INCLUDE})
link_directories(${PROJECT_NAME} ${PCL_LIBRARY_DIRS} ${SOURCES} )
add_definitions(${PCL_DEFINITIONS})
add_executable (${PROJECT_NAME} src/main.cpp)
target_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS} ${PCL_LIBRARIES} ${SOURCES})
In my file helper.cpp I have:
#include <pcl/io/pcd_io.h>
Which gives the error:
fatal error: 'pcl/io/pcd_io.h' file not found
#include <pcl/io/pcd_io.h>
^~~~~~~~~~~~~~~~~
However I have the same include in main.cpp with no errors.
I would be very grateful for any help, please let me know if I need to clarify my question or error.
Thank you.
There are several errors in the CMakeLists.txt with the following changes the project loads appropriate libraries and builds properly. Another note is that before, to include helper.h I needed to write:
#include "../include/helper.h".
Now it works as expected with #include "helper.h".
Here is the modified CMakeLists.txt:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(object_detection)
find_package(PCL 1.5 REQUIRED)
find_package(OpenCV REQUIRED)
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)
include_directories(${PCL_INCLUDE_DIRS} include)
link_directories(${PROJECT_NAME} ${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
#add_executable (${PROJECT_NAME} src/helper.cpp src/main.cpp )
add_executable (${PROJECT_NAME} ${SRC_FILES} )
target_link_libraries (object_detection ${PCL_LIBRARIES} ${OpenCV_LIBS})