Ask Your Question

Best way to include a header generated with configure_file

asked 2014-04-17 05:02:17 -0500

po1 gravatar image

updated 2016-10-24 09:00:07 -0500

ngrennan gravatar image

Hi there,

In a few packages, especially when porting third-party software to catkin, I encounter the problem of auto-generated headers. In a single-package scenario, it is pretty easy to handle, as I just have to play with include_directories() and the matter is settled. As for the install space, an appropriate install() rule easily takes care of it. Things get more complicated when dealing with devel space. For example, I would like my fooConfig.cmake to contain a link to the devel space include folder, that would contain the header generated from a template with configure_file(), for example. The same goes with pkg-config files.

I looked around and could not find any documentation or best practices on the matter. So far, I have tested adding a cmake/ file like the following:


and then in my CMakeLists.txt I have something akin to this:

  CFG_EXTRAS foo-extras.cmake

set(devel_pkg_inc ${CATKIN_DEVEL_PREFIX}/include/${PROJECT_NAME})

configure_file( ${devel_pkg_inc}/foo.h)

It does the job for me, but it looks a bit clunky. Is there any catkin-ish way of doing this? Am I missing anything obvious?

edit retag flag offensive close merge delete


demmeln gravatar image demmeln  ( 2014-04-18 01:41:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-04-18 10:23:18 -0500

Dirk Thomas gravatar image

The proposed way is ok. Generating the header into the devel space is exactly what should be done. Also the install rule is necessary - as is.

But you can achieve the same functionality without an extra file. First to make it work for code compiler in your package you need to add the include folders:

# also assuming that you also have static headers in the include folder of your package
include_directories(include ${CATKIN_DEVEL_PREFIX}/include)

You should always only add the include folder (rather then the package specific subfolder in it) and then include your code with the package name as a namespace (e.g. #include <pkgname/myheader.h).

Second to make packages work which depend on yours you have to export both include locations:

catkin_package(INCLUDE_DIRS include ${CATKIN_DEVEL_PREFIX}/include)

For the install case it will work correctly since the include folder in install space will contain your static headers form the package include folder (if you have also installed them) as well as the header installed from the devel space. Again keep in mind that all your headers should be in a subfolder of include and should also be referenced like this in code.

edit flag offensive delete link more


Thanks! This is exactly what I wanted to know. To be honest, I was looking at INCLUDE_DIRS but was thinking that introducing a devel space path in it might not play nicely with the install space rules. Now I know that it's fine, and getting rid of this -extras.cmake file is a relief.

po1 gravatar image po1  ( 2014-04-19 10:42:01 -0500 )edit

Unfortunately, it did not work: `CMake Error at /opt/ros/hydro/share/catkin/cmake/catkin_package.cmake:336 (message): catkin_package() include dir '/Users/paul/test_ws/devel/include' is neither an absolute directory nor exists relative to '/Users/paul/test_ws/src/foo'`

po1 gravatar image po1  ( 2014-04-28 03:11:13 -0500 )edit

I had to add this before catkin_package: file(MAKE_DIRECTORY ${CATKIN_DEVEL_PREFIX}/include)

po1 gravatar image po1  ( 2014-05-05 00:23:20 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-04-17 05:02:17 -0500

Seen: 3,085 times

Last updated: Apr 18 '14