Ask Your Question
0

Should I put all header files inside the include dir?

asked 2018-05-09 05:06:01 -0600

Mbuijs gravatar image

I've been trying to find the answer to this question using the search function, but only came accross questions about missing header files etc.

Background: in the past 10 years of coding C/C++ I've learned that it's important to think about the (external) interface of the code I write. It's generally a good idea to keep it small and simple, hiding implementation details as much as I can. As a result, typically a software package/library that I would develop would have an include directory that only contains the header files that I want to expose to the users of that package/library, while the rest of the headers that are only intended for internal use are located in the src directory.

Now I'm getting started with ROS and I see quite a few packages that seem to put .h files in include and .cpp files in src. Is there are specific reason to do this?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-05-09 05:55:18 -0600

gvdhoorn gravatar image

updated 2018-05-09 05:58:01 -0600

This will be an opiniated answer, but just to provide you with my own insight / experience:

Now I'm getting started with ROS and I see quite a few packages that seem to put .h files in include and .cpp files in src. Is there are specific reason to do this?

include directories are only exported (ie: added to the public interface of a ROS package) if that directory is explicitly passed to the catkin_package(.. INCLUDE_DIRS ..) call as an argument. Packages which have no headers that should be part of the public interface should not do this. Packages which just provide 'top level' nodes, but no libraries are examples of pkgs that should probably not have their include directory exposed by catkin_package(..).

So if we accept that, any header in $pkg_dir/include (or any other folder with headers that is passed to catkin_package(..)) always defines the public interface of a package. And inverting this: $pkg_dir/include should only contain headers that are intended to be part of that same public interface (again: assuming that $pkg_dir/include is actually passed to catkin_package(..)).

Now as to whether this 'best practice' is adopted by the ROS community as a whole I cannot say. It is however how I've structured my packages, and it works well for me.

I believe this would align with what you already wrote yourself: only add public headers to a publicly-visible include folder, and keep the rest private. So I would recommend you to keep doing what you already have been doing, and consider the $pkg_dir/include directory as the default location for the public interface of any/your package(s).

edit flag offensive delete link more

Comments

Note that there are other techniques to hide implementation details (that you're probably familiar with). Following a specific file/directory layout/structure is a rather coarse, but effective, first step.

gvdhoorn gravatar imagegvdhoorn ( 2018-05-09 05:56:41 -0600 )edit

Thanks for the extensive explanation.

The first packages I was setting up were 'top level' nodes, so I did not pass include to catkin_package(..). In practice I didn't have any public header files and therefore didn't even create the include dir, instead just put everything in src.

Mbuijs gravatar imageMbuijs ( 2018-05-09 15:11:05 -0600 )edit
0

answered 2018-05-09 05:51:31 -0600

Your 10 years of experience is correct. There is no need to put all of a packages header files in the include directory. The include directory will contain header files that are needed by external packages, so the .h files generated from messages and services as well as the external interfaces of any exported shared objects.

When you do a catkin_make install only header files within the include directory will be installed.

edit flag offensive delete link more

Comments

1

When you do a catkin_make install only header files within the include directory will be installed.

only if the build script contains the necessary install(..) statements.

gvdhoorn gravatar imagegvdhoorn ( 2018-05-09 05:57:12 -0600 )edit

That's true I should have stated, if you've setup installation correctly in the build script.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-09 08:46:09 -0600 )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

Stats

Asked: 2018-05-09 05:06:01 -0600

Seen: 798 times

Last updated: May 09 '18