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

how to use a header only package?

asked 2020-09-11 08:25:49 -0500

rephaxe gravatar image
└── my_package
   ├── CMakeLists.txt
   ├── include
   │   └── my_header.h
   └── package.xml

Hi, I have to use a provided package whose file structure is given above. I tried to include header file in another executable node but could not build it because of "undefined reference" errors. How can i use it?

edit retag flag offensive close merge delete

Comments

Can you provide us with exact the errors here please?

JackB gravatar image JackB  ( 2020-09-11 12:33:55 -0500 )edit

@rephaxe, to solve this either you force the compiler to compile an empty .cpp including only the header (for the compiler to copy all header content in the cpp) or define the compile task of the header as an interface.

Weasfas gravatar image Weasfas  ( 2020-09-12 07:28:14 -0500 )edit

As I understand it, the header #include will copy and paste the code directly into his node.cpp and make it directly part of the executable. Maybe what is happeneing is some linked code is trying to access the functions defined in the executable in another cpp file and it is unable to find them. Without the error message copy and pasted here it is hard to say.

JackB gravatar image JackB  ( 2020-09-12 11:21:14 -0500 )edit
1

Yes, but the problem is that the compiler always throws an error if there is no cpp to compile, since you have only a header the cmake directive throws the error. For that reason a possible solution is having an empty cpp. The other one is declaring the header as interface, but when I had this particular problem I tried to use that approach and failed. As a clarification I will explain my experience with this problem. In one project I had a header only file and then a main node and a library, I was able to compile and execute properly the package by including the header only in the library cpp and then the library header on the main node cpp. And that is becasue the header content was copied throught the entire cpp compilation files. In fact there are versions of catkin in which this is not supported.

Weasfas gravatar image Weasfas  ( 2020-09-13 07:04:16 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-09-13 21:43:27 -0500

msmcconnell gravatar image

When trying to create a header only catkin package you need to do two things.

  1. Mark the header as a required include for dependent packages using catkin_package
  2. Add an install tag for the header

This looks like the following in the CMakeLists.txt file

catkin_package(
  INCLUDE_DIRS include
)

install(DIRECTORY include/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)

I believe the above is all you need, but if you run into issues you can also try adding the following between your catkin_package and install tags.

include_directories(
  include
)

Once your header package is good to go you just need to add it as a dependency for your executable

find_package(catkin REQUIRED COMPONENTS
  my_package
)

add_executable( my_executable_name src/my_source.cpp)

add_dependencies(my_executable_name ${catkin_EXPORTED_TARGETS})

Don't forget to also add it in the package.xml

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2020-09-11 08:25:49 -0500

Seen: 1,256 times

Last updated: Sep 13 '20