cmakeroscatkin

Unable to include a header-only library in ROS


I have the following project structure:

utils 
 include
   utils 
    image_utils.h
 CMakelists.txt
 package.xml
graph
 src
  graph.cpp
 CMakelists.txt
 package.xml

I am trying to use image_utils.h in graph.cpp.

The CMakelists inside utils looks like this:

cmake_minimum_required(VERSION 3.0.2)
project(utils)

find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS roscpp 
)
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

add_library(${PROJECT_NAME} INTERFACE)
target_link_libraries(${PROJECT_NAME} INTERFACE ${catkin_LIBRARIES} )

install(
  DIRECTORY include/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  FILES_MATCHING PATTERN "*.h"
)

And in the CMakelists inside graph i call find_package(catkin REQUIRED COMPONENTS roscpp) as seen below:

cmake_minimum_required(VERSION 3.0.2)
project(pose_graph)

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")

find_package(catkin REQUIRED COMPONENTS roscpp std_msgs cv_bridge utils)

find_package(OpenCV 3.4 REQUIRED)

include_directories(${catkin_INCLUDE_DIRS} )
catkin_package(CATKIN_DEPENDS utils)
add_executable(graph src/graph.cpp)
target_link_libraries(graph ${catkin_LIBRARIES} ${OpenCV_LIBS} utils)

The package.xml of graph has the following relevant tag:

<depend>utils</depend>

My error message is:

graph.cpp:12:10: fatal error: utils/image_utils.h: No such file or directory
 #include "utils/image_utils.h"
          ^~~~~~~~~~~~~~~~~~~~~

Any idea what I am doing wrong here?


Solution

  • Finally figured it out. Here are my CMakelists files:

    utils:

    cmake_minimum_required(VERSION 3.0.2)
    project(utils)
    
    find_package(catkin REQUIRED COMPONENTS roscpp)
    
    catkin_package(
      INCLUDE_DIRS include
      CATKIN_DEPENDS roscpp
    )
    
    include_directories(
      include
      ${catkin_INCLUDE_DIRS}
    )
    
    install(
      DIRECTORY include/${PROJECT_NAME}/
      DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
      FILES_MATCHING PATTERN "*.h"
    )
    

    graph:

    cmake_minimum_required(VERSION 3.0.2)
    project(pose_graph)
    
    set(CMAKE_BUILD_TYPE "Release")
    set(CMAKE_CXX_FLAGS "-std=c++11")
    
    find_package(catkin REQUIRED COMPONENTS roscpp std_msgs cv_bridge utils)
    
    find_package(OpenCV 3.4 REQUIRED)
    
    catkin_package(CATKIN_DEPENDS utils)
    include_directories(${catkin_INCLUDE_DIRS} )
    
    add_executable(graph src/graph.cpp)
    
    target_link_libraries(graph ${catkin_LIBRARIES} ${OpenCV_LIBS})