cmake subdirectory preprocesser include error - c++

.
├── 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?

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.

'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})

How to include external modules headers when FetchContent_Declare is used?

I want to use modern CMake project structure convention in my new project to follow good practices and to be consistent with many other projects, but I'm facing problem with include path.
My directories structure looks as follows:
.
├── build
├── CMakeLists.txt
├── extern
│ └── CMakeLists.txt
└── src
├── CMakeLists.txt
└── main.cpp
I want to use FetchContent_Declare instead of git submodules to manage the third-party libraries.
The root CMakeLists.txt:
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
set(ProjectName "MyApp")
project(${ProjectName})
add_subdirectory(extern)
add_subdirectory(src)
The extern CMakeLists.txt:
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
include(FetchContent)
FetchContent_Declare(
extern_spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.8.2)
FetchContent_GetProperties(extern_spdlog)
if(NOT extern_spdlog_POPULATED)
FetchContent_Populate(extern_spdlog)
add_subdirectory(
${extern_spdlog_SOURCE_DIR}
${extern_spdlog_BINARY_DIR}
)
endif()
The src CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
set(BINARY ${CMAKE_PROJECT_NAME})
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)
set(SOURCES ${SOURCES})
add_executable(${BINARY}_run ${SOURCES})
add_library(${BINARY}_lib STATIC ${SOURCES})
target_link_libraries(${BINARY}_run extern_spdlog)
And the main.cpp:
#include <spdlog/spdlog.h>
int main(int argc, char* argv[]) {
spdlog::info("Welcome to spdlog!");
return 0;
}
CMake executes and the spdlog library is cloned properly and can be found in the build directory at: build/_deps/extern_spdlog-src.
If I call make in the build directory then I get fatal error: spdlog/spdlog.h: No such file or directory.
How to add include directories for the external libraries used in the application?
I was initially confused about this as well. It doesn't help that the names of the FetchContent targets often match the names of the imported targets. What worked for me was similar (though not identical) to what the first comment states. I used target_link_libraries(target spdlog::spdlog) (if you want to use the pre-compiled library or spdlog::spdlog_header_only if you want to use the header source only version.

CLion can't find CMake generated headers?

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"