Question

In my application I use a static library with header file foo.h

In my build script I use a gcc -I flag -I./lib.

The foo library is in a directory ./lib/foo. In my main application I include foo.h as

#include "foo/foo.h"

Now I'm refactoring part of my application as a library, bar, that I expect to statically link to another application. This library bar depends on foo. With my current project layout, I could include foo in bar as

#include "foo/foo.h"

However, that would force users of bar to place foo in a directory called foo.

Is the standard thing to do in this case the following?

Add an -I flag to the build script that allows including foo in bar with just

#include "foo.h"
Was it helpful?

Solution

The reason for using folder structures with include files is to prevent collisions with header files of the same name. For instance, lets say I am using a library for encryption, and they have a header file called "status.h". At the same time I want to use a messaging library, and they have also have a header file named "status.h". They cannot coexist in the same directory and if you make the directory an include path it may not know which one you want to include. If you keep them seperate you can include both:

#include "encryption/status.h"
#include "messaging/status.h"

So the answer to your question depends on how likely you think the name of the include files will conflict with other include files of other libraries. If it will never conflict then what you are suggesting is fine. If it will conflict them make them use the directory.

Happy coding!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top