14

I have several libraries made by myself (a geometry library, a linked list library, etc). I want to make a header file to include them all in one lib.h. Could I do something like this:

#ifndef LIB_H_
#define LIB_H_

#include <stdio.h>
#include <stdlib.h>
#include <linkedlist.h>
#include <geometry.h>
....

#endif

Then I could just reference this one library and actually reference multiple libraries. Is this possible? If not, is there a way around it?

3 Answers 3

21

Yes, this will work.

Note, however, that if you include a lot of headers in this file and don't need all of them in each of your source files, it will likely increase your compilation time.

It also makes it more difficult to figure out on which header files a particular source file actually depends, which may make code comprehension and debugging more difficult.

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

3 Comments

It could also be a good thing if this lib.h where to be used as a precompiled header if the compiler support it (gcc does)
I was just using this as an example, I will probably only use this method for my libraries and not C libraries.
I'm currently trying to port a game, as well as a support library, from windows to linux -- because of the "a single header to rule them all"-method it's a nightmare. Compilation time is massive and figuring out where anything belongs is virtually impossible. ANY change in any header file (and I need to do a lot of those) will force all source files to recompile. On top of that it's impossible to compile some header files (to check for errors) because they don't include anything themselves.
5

Yes, this works, and is in fact used in most APIs. Remember what a #include actually does (tell the preprocessor to immediately include a new file), and this should make sense. There is nothing to prevent several levels of inclusion, although implementations will have a (large) maximum depth.

As noted, you should organize your headers into logical groupings.

1 Comment

Is there a name for these headers that only include other headers? I like your answer, +1
4

The "right way" to include (in A)

  • do nothing if: A makes no references at all to B
  • do nothing if: The only reference to B is in a friend declaration
  • forward declare B if: A contains a B pointer or reference: B* myb;
  • forward declare B if: one or more functions has a B object/pointer/reference as a parameter, or as a return type: B MyFunction(B myb);
  • #include "b.h" if: B is a parent class of A
  • #include "b.h" if: A contains a B object: B myb;

From a cplusplus.com article you should definitively read

3 Comments

What about templates TemplateClass<B> mytemplateb;?
I am not a guru, but both are classes (TemplateClass and B). Important is how you use them. Check the list above. Even its a template I dont think you need the definition if you dont need to know it. So probably you need inclusion only in case of value type aggregation or inheritance. Else a forward declaration should suffice. Yes templates can be forward declared too.
The question is not about C++

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.