tags:

views:

98

answers:

2

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?

A: 

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
+4  A: 

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
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
I was just using this as an example, I will probably only use this method for my libraries and not C libraries.
Mohit Deshpande