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

Including custom header file in ROS2 CMakeList

asked 2021-09-14 01:40:20 -0500

KimJensen gravatar image

updated 2021-09-14 04:59:31 -0500

Hello everyone,

I'm trying to create a ROS2 package, which has a custom header file, and links to other header files downloaded from GitHub (in this case WiringPi).

For doing this, I understand that I must place my custom header file inside of the /include/package_name directory, but I cannot link it to the source file. I'm trying to following the documentation of ament_cmake for "Adding files and headers", "Adding dependencies" and "Building a Library". Honestly, I don't really understand the documentation very well, and the lack of examples are making it hard for me to implement it myself.

Could anyone assist me with understanding how I can link my header file to my source file (e.g. through a sample CMakeList/package.xml), and with adding e.g. the WiringPi to the code?

I'm currently following this post, and using the same files+filenames, but the source file still isn't able to find the header file.

Ubuntu 20.04, ROS2 Foxy, C++

Thank you in advance, for any insights that could lead to me overcoming my problem.

My CmakeList.txt

cmake_minimum_required(VERSION 3.5)
project(tester123)

# 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()

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

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()

add_executable(main_node src/minimal_publisher_node.cpp src/minimal_publisher.cpp)
ament_target_dependencies(main_node rclcpp std_msgs)

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

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

install(
  DIRECTORY include/
  DESTINATION include
)

ament_package()

Error code (during colcon build)

tester123/src/minimal_publisher.cpp:1:10: fatal error: minimal_publisher.hpp: No such file or directory
1 | #include "minimal_publisher.hpp"
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-09-17 01:10:53 -0500

janindu gravatar image

updated 2021-09-17 01:11:17 -0500

Assuming your directory structure is something like this,

tester123

  • src/
  • CMakeLists.txt
  • package.xml

Create another directory at tester123/include/

Add the minimal_publisher.hpp inside the include directory.

Before the add_executable line in the CMakeLists.txt, add the following line

include_directories(include)

The compiler should be able to find the minimal_publisher.hpp now.

edit flag offensive delete link more

Comments

It's usually these small details that I tend to miss. Thank you for your time and the easy to understand answer!

KimJensen gravatar image KimJensen  ( 2021-09-20 03:07:09 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2021-09-14 01:40:20 -0500

Seen: 3,562 times

Last updated: Sep 17 '21