ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros2 foxy including header file from other package

asked 2021-11-03 07:43:21 -0600

morten gravatar image

updated 2021-11-04 05:38:49 -0600

I have two packages, temp_lib and temp_pkg. In temp_lib I have a file temp.hpp that I would like to include in other packages, but when I attempt to do so by writing in #include "temp_lib/temp.hpp" in a cpp file in temp_pkg then it says this header cannot be found.

The problem is i'm not really sure how to set up the CMakeLists file for the library.

How can I set these packages up for this to be possible?

LIBRARY CMAKE

cmake_minimum_required(VERSION 3.5)
project(temp_lib)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

install(TARGETS
  DESTINATION lib/${PROJECT_NAME}
  PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}/
  )

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

ament_package()

temp.hpp (from temp_lib)

#ifndef TEMP_LIB__TEMP_HPP_
#define TEMP_LIB__TEMP_HPP_

#include <iostream>

#endif // TEMP_LIB__TEMP_HPP_

PKG CMAKE

cmake_minimum_required(VERSION 3.5)
project(temp_pkg)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(temp_lib REQUIRED)

include_directories(
  ${temp_lib_INCLUDE_DIRS}
)

add_executable(test src/temp.cpp)
ament_target_dependencies(test rclcpp temp_lib)

install(TARGETS
  test
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()

temp.cpp (from temp_pkg)

#include "temp_lib/temp.hpp"
int main(int argc, char *argv[]) {
  std::cout << "it works" << std::endl;
  return 0;
}

Edit: for legibility I removed some repeated standard parts of the CMake files, these are

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

and

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()
edit retag flag offensive close merge delete

Comments

I think @morten's answer is a good example using ament_cmake (and I see no reason not to).

One question that's potentially just a typo. I did notice that your example temp_lib install function only installs *.h headers rather than *.hpp headers. If that's the case then temp_lib/temp.hpp won't be installed since it doesn't patch the pattern.

nuclearsandwich gravatar image nuclearsandwich  ( 2021-11-05 17:54:57 -0600 )edit

that was definitely a typo

morten gravatar image morten  ( 2021-11-23 07:57:41 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-04 07:00:34 -0600

morten gravatar image

updated 2021-11-23 07:57:08 -0600

I'm not sure is this is the "recommended" way to do it, as I am using ament commands instead of pure cmake. If someone knows how to do a pure cmake alternative I would be interested to hear.

But a possible solution I have found is with:

lib cmake

cmake_minimum_required(VERSION 3.5)
project(temp_lib)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

install(
  DIRECTORY include/
  DESTINATION include
)

ament_export_include_directories(include)

ament_package()

pkg cmake

cmake_minimum_required(VERSION 3.5)
project(temp_pkg)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(temp_lib REQUIRED)

add_executable(test_case src/temp.cpp)
ament_target_dependencies(test_case rclcpp temp_lib)

install(TARGETS
  test_case
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()

Edit: for legibility I excluded

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()
edit flag offensive delete link more

Comments

In the pkg cmake, wouldn't you also need to include...

include_directories(
  ${temp_lib_INCLUDE_DIRS}
)

...before the add_executable directive?

swiz23 gravatar image swiz23  ( 2022-08-02 22:16:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-11-03 07:43:21 -0600

Seen: 2,104 times

Last updated: Nov 23 '21