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

Include Header file from another package - Indigo

asked 2015-01-28 16:08:36 -0600

Pototo gravatar image

updated 2015-01-28 16:39:22 -0600

Hi folks,

I have a file called (every name here is FAKE) file.h from the package named "my_package."

Now, main.cpp located inside package named "the_package" needs to include the library in this manner:

#include <file.h>

but "the_package" cannot find file.h How do I make catkin allow "the_package" to find file.h without having to do something like:

#include "../../../my_package/file.h"

I am assuming I have to do it in my CMakeList.txt, but using find_package() nor find_package(catkin Required ...) are doing it.

Every package is located inside my "catkin_ws" workspace.

What is the proper way?


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-01-28 16:39:42 -0600

William gravatar image

updated 2015-01-28 16:40:10 -0600

You must install the header file.h from the package called my_package. You can do that by using this CMake snippet in the CMakeLists.txt file in the my_package package:

## Install project namespaced headers
install(DIRECTORY include/${PROJECT_NAME}/

Note that this assumes your headers, in this case file.h, are in your package under the include/<project name> folder, which for my_package would be include/my_package/file.h. This is a good practice, since all header files for all packages can be installed to the same place and you wouldn't want your file.h and another package's to collide. This also means that the main.cpp in the_package should do #include <my_package/file.h> instead of what you listed above.

Then the the_package package must:

  • have a build_depend on my_package in its package.xml
  • have called find_package(my_package REQUIRED) in its CMakeLists.txt
  • add my_package's exported include directories by doing include_directories(${my_package_INCLUDE_DIRS})

The include_directories(...) CMake call needs to come after the find_package(...) call.

Each of those points above ensures that:

  • my_package is built before the_package
  • my_package exists and can be found through CMake
  • the location of the headers exported by my_package is passed to the compiler for the libraries and executables in the_package

Note that you can also do find_package(catkin REQUIRED COMPONENTS my_package ...) instead of find_package(my_package REQUIRED), where the ... maybe other packages that the_package depends on. Doing this would allow you to use the catkin_ prefixed CMake variables, so you would do include_directories(${catkin_INCLUDE_DIRS}) rather than include_directories(${my_package_INCLUDE_DIRS}).

edit flag offensive delete link more


The installation process needs to be done for the all the related files of which the_package depends on. That includes doing it for the packages that my_package depends on as well. Super recursive process haha.


Pototo gravatar image Pototo  ( 2015-01-28 20:15:34 -0600 )edit

Each one has to follow the procedure correctly, but it's the same for them all, except that <build_export_depends> may be required when there are transitive dependencies.

joq gravatar image joq  ( 2015-01-28 21:17:56 -0600 )edit

If you are chaining these libraries across packages then, as @joq said, you'll need to make sure you declare the build_export_depends so that transitive dependencies are respected. I didn't mention transitive dependencies because you didn't say you were doing that in your original question.

William gravatar image William  ( 2015-01-29 00:22:23 -0600 )edit

This documentation should help:

William gravatar image William  ( 2015-01-29 00:26:15 -0600 )edit

I tried this and doesn't work, because in CMakeLists.txt of my_package is missing:

  INCLUDE_DIRS include

with yours steps and adding this resolve the problem.

Raul Gui gravatar image Raul Gui  ( 2015-06-19 12:10:43 -0600 )edit

but if you put: catkin_package( INCLUDE_DIRS include LIBRARIES create_lib CATKIN_DEPENDS roscpp rospy std_msgs DEPENDS system_lib ) you get a CMake Error: Project 'the_package' tried to find library 'my_package'. The library is neither a target nor

Raul Gui gravatar image Raul Gui  ( 2015-06-19 12:37:53 -0600 )edit

After following the above steps, I get the following error.

CMake Error at /opt/ros/kinetic/share/catkin/cmake/custom_install.cmake:13 (_install):  install DIRECTORY given no DESTINATION!  Call Stack (most recent call first): CMakeLists.txt:19 (install)
yash_5 gravatar image yash_5  ( 2018-11-07 18:12:04 -0600 )edit

@yash_5 you should probably create a new question and reference this. This is a fairly old question and the you'll probably get a better response by making a new one.

jayess gravatar image jayess  ( 2018-11-07 18:22:08 -0600 )edit

answered 2015-01-28 16:47:06 -0600

joq gravatar image

updated 2017-04-10 17:31:57 -0600

First, "my_package" should export the library and its header file like this:

The header should be in include/my_package/file.h, and will be referenced like this:

#include <my_package/file.h>

Any packages including that header needs to follow the relevant parts of these directions:

edit flag offensive delete link more



I like this answer better, it links to the docs, which is what I should have done.

William gravatar image William  ( 2015-01-28 17:07:12 -0600 )edit

Yeah, I was looking for that documentation for a few weeks now. Google would always point me to the very general catkin link on the website.

Thanks for pointing out the direct documentation linked. I found my answer

Pototo gravatar image Pototo  ( 2015-01-28 20:17:07 -0600 )edit

Question Tools



Asked: 2015-01-28 16:08:36 -0600

Seen: 27,714 times

Last updated: Apr 10 '17