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?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

3 Answers3

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 1
    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) – epatel Apr 20 '10 at 23:28
  • I was just using this as an example, I will probably only use this method for my libraries and not C libraries. – Mohit Deshpande Apr 20 '10 at 23:46
  • 2
    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. – Clearer Dec 14 '14 at 15:43
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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
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

ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
  • What about templates `TemplateClass mytemplateb;`? – TobiMcNamobi Jun 08 '17 at 07:18
  • 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. – ManuelSchneid3r Jun 08 '17 at 14:01
  • The question is not about C++ – martinkunev Jun 28 '19 at 08:29