Problem:
I am struggling to link the header files in the project correctly using CMake lists. All the example online seem to be simpler and the projects use "Modern CMake" with relations which I’m also unfamiliar with.
Error:
warn
XXXXX.hpp is not a directory
fatal error
#include XXXXX.hpp: No such file or directory
Back Ground Statement:
The root dir is a node within a larger ROS based project utilising catkin tools. It is a Unix system Ubuntu 20.04. I will replicate the system directory using generic titles below, it will include all build related files ( I can expand where necessary upon request ).
File sys:
Details of CMakeLists.txt
root_dir/CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
project(project_name LANGUAGES CXX)
add_subdirectory(${PROJECT_SOURCE_DIR}/src/orange ${PROJECT_BINARY_DIR}/orange)
add_executable(${PROJECT_NAME}_node src/project_name_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
orange-lib
)
root_dir/include/project_name/orange/CMakeLists.txt
add_library(orange-lib navel.cpp blood.cpp tangelo.cpp)
// as per Friedrich suggestion
include_directories(orange-lib PUBLIC ${PROJECT_SOURCE_DIR}/include/project_name/orange)
3
Answers
Your root_dir/include/project_name/orange/CMakeLists.txt should be
You don’t want to provide the full path to the file as include directory, but the path to the parent directory
/full/path/to
. In your cpp file you then can useIn fact, you can throw a lot of superfluous code out of
root_dir/include/project_name/orange/CMakeLists.txt
:See the documentation of
target_include_directories
for reference.In fact, we should be able to do without
${PROJECT_SOURCE_DIR}
. An alternative would be to just give the relative path (including..
to go up some levels). Or consider puttingorange-lib
‘s includes next to the sources.Your
root_dir/include/project_name/orange/CMakeLists.txt
should look like this:Part of the problem is that your directory structure is badly designed.
include/...
should start from same level as respectiveCMakeLists.txt
and you have to reach to parent directory.