When trying to create a header only catkin package you need to do two things.
- Mark the header as a required include for dependent packages using catkin_package
- 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
Can you provide us with exact the errors here please?
@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.
As I understand it, the header
#include
will copy and paste the code directly into hisnode.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.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.