Originally Posted by
127.0.0.1
One is the idea of multiple inclusions - I have tried to find articles using Google that talked about the details of multiple inclusions (their impact on performance, how to minimize them, etc) but have been unsuccessful.
If you include something multiple times and don't get errors, there will be no impact on performance, etc, or at least not with a decent compiler.
If you get errors, there is no performance.
Errors are usually due to "redefinition". There are a few ways to eliminate that. The first is to only include files in the places you actually need them. This seems obvious, but consider: you have a .c file with main() in it, and it #includes "lib.h", which is a header for a separate .c file that will be compiled and linked to main(). lib.c uses some functions from <string.h>, but lib.h is just function prototypes, typedefs, etc. Since "string" is not a datatype in C, those prototypes cannot possibly require <string.h>; it's the function definitions that require it. In that case, #include <string.h> in lib.c, not in lib.h.
This is kind of an irrelevant example since you can include string.h as much as you want, it will not result in redefinition errors or wasted resources, but it's the principle that counts: put your includes in your .c files first, and only move them to the header if the compiler throws an error on the header because (eg) it refers to a datatype that requires a .h.
When you do get unavoidable redefinition errors, because you have a type defined in fileA.h that's used in the prototypes in fileB.h, and a type defined in fileB.h is used in fileA.h, use #define and #ifndef:
Code:
/* fileA.h */
#define FILEA_INC 1
#ifndef FILEB_INC
#include "fileB.h"
#endif
/* fileB.h */
#define FILEB_INC 1
#ifndef FILEA_INC
#include "fileA.h"
#endif
Now, when fileA.h is included from whereever, it will not #include itself again via fileB.h.