0

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"
1
  • Sounds sensible. The #include file names (and any hierarchy) should organize logically, not by random implementation details. Commented Mar 14, 2014 at 1:49

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't consider that, thanks. In this case foo.h has quite a unique name, so I think I'll go with my proposed solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.