skip to Main Content

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:

File Sys Diagram

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


  1. Your root_dir/include/project_name/orange/CMakeLists.txt should be

    project(project_name LANGUAGES CXX)
    
    
    set(INCLUDE_DIRS
        /full/path/to
    )
    
    set(CXX_SRCS
        navel.cpp
        blood.cpp
        tangelo.cpp
    )
    
    
    add_library(orange-lib ${CXX_SRCS})
    
    include_directories(${INCLUDE_DIRS})
    

    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 use

    #include "navel.hpp"
    #include "blood.hpp"
    #include "tangelo.hpp"
    
    Login or Signup to reply.
  2. In fact, you can throw a lot of superfluous code out of root_dir/include/project_name/orange/CMakeLists.txt:

    # no project() here, it suffices to have it once
    
    # just use the source file names
    add_library(orange-lib navel.cpp blood.cpp tangelo.cpp)
    # could also use a relative path below
    target_include_directories(orange-lib PUBLIC ${PROJECT_SOURCE_DIR}/include/project_name/orange)
    

    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 putting orange-lib‘s includes next to the sources.

    Login or Signup to reply.
  3. Your root_dir/include/project_name/orange/CMakeLists.txt should look like this:

    add_library(orange-lib)
    set(public_headers_path <directory where public headers are>)
    target_sources(orange-lib
        navel.cpp
        blood.cpp
        tangelo.cpp
        ${public_headers_path}/navel.hpp
        ${public_headers_path}/blood.hpp
        ${public_headers_path}/tangelo.hpp 
    )
    
    target_include_directories(orange-lib PUBLIC ${public_headers_path})
    

    Part of the problem is that your directory structure is badly designed. include/... should start from same level as respective CMakeLists.txt and you have to reach to parent directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search