Why not multiple inclusion of a header file if certain precautions are taken?

  • Thread starter Thread starter Eus
  • Start date Start date
  • Tags Tags
    File Multiple
Click For Summary
SUMMARY

The discussion centers on the implications of multiple inclusions of header files in C programming. While the GCC compiler allows multiple inclusions of function prototypes without error, including struct, enum, or typedef declarations can lead to compilation errors due to redefinition. The consensus is that to avoid potential conflicts and ensure code stability, developers should always use include guards, such as the #ifndef and #define preprocessor directives, to prevent multiple inclusions of header files.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with GCC compiler behavior
  • Knowledge of preprocessor directives in C
  • Experience with header file organization and structure
NEXT STEPS
  • Implement include guards in custom header files using #ifndef and #define
  • Explore the C preprocessor and its directives in depth
  • Learn about best practices for organizing header files in large C projects
  • Investigate the implications of header file inclusion on compilation time and memory usage
USEFUL FOR

C developers, software engineers working with C, and anyone involved in maintaining or optimizing C codebases will benefit from this discussion.

Eus
Messages
93
Reaction score
0
Hi Ho!

I often heard people say that we should avoid multiple inclusion of a header file.
And, they very often show how to do it.
But, I myself haven't yet found the reason of not to do it when certain precautions are taken.

What I am going to discuss here is C programming, not C++.

I included one single header file containing:
- function prototype
- struct declaration
- enum declaration
- typedef
multiple times in several source code files, but my gcc happily compiled the source code files.

Of course, if I added one variable definition within the header file (e.g. int x = 0), gcc complained of multiple variable declarations.

But, I know that a variable should be best defined in a source code file not in a header file.

Therefore, as far as I don't put any variable definition in a header file, there is no problem with the multiple inclusion of the header file, isn't there?

Is there any reason to say that multiple inclusion of a header file should be avoided at all?

What do you think?

Thank you.


Eus
 
Technology news on Phys.org
I think, the c preprocessor takes the last definition of a meta variable.
 
The standard C library header files - like stdlib.h - look like this, simplfied a LOT:
Code:
#ifndef __STDLIB__
#define __STDLIB__
stdlib stuff goes in here
...
...
#endif /* __STDLIB__ */
This prevents the header from causing duplicate symbol errors, etc because if the preproccesor tries to include stdlib.h a second time, 99.5% of it is skipped over.
You can read your header files and see how this is implemented for your compiler.

None of the include "defines" like __STDLIB__ are defined anywhere else. Don't you do it either.

If you need to use a macro directive like _INCLUDE_POSIX_SOURCE to have a proper global effect, it has to appear in your code before any include whatsoever.
 
Also, because of the information above, what do you think you will you accomplish by including STDC header files more than one time? All bets are off on homegrown header files that do not follow the above convention.
 
Eus said:
Is there any reason to say that multiple inclusion of a header file should be avoided at all?
Suppose you have some header file foo.h that defines a structure and prototypes a function that uses the structure. A source file that includes foo.h twice as follows will fail to compile:
Code:
#include "foo.h"
#include "foo.h"
void foo (struct foo * foo_ptr)
{
   ...
}

This, on the other hand, will compile:
Code:
#include "foo.h"
void foo (struct foo * foo_ptr)
{
#include "foo.h"
   ...
}

The difference is in scope. It is perfectly valid in C to use the same name in different scopes. The first #include occurs at file scope, while the second occurs in function scope. Note well: While it is valid to do this, it is not very smart (exception: obfuscations such as this help in winning the IOCCC).

basePARTICLE said:
I think, the c preprocessor takes the last definition of a meta variable.
The c preprocessor is not that "smart". All that #include "foo.h" means is to replace the #include line with the body of foo.h. If the code says to do it twice, the preprocessor will do it twice. Hmm. I wonder if anyone has written a IOCCC entry that uses multiple includes in a clever way?
 
Hi Ho!

D H said:
Suppose you have some header file foo.h that defines a structure and prototypes a function that uses the structure. A source file that includes foo.h twice as follows will fail to compile:

Ah, you are right!
Because what I did was to include a header file once in each source file (this was what I thought as multiple inclusion of a header file), I did not get any error.
But, when I included a header file two times in a single source file like what you have illustrated, I got an error.

Of course, the error is because redefinition of the struct. Whereas, multiple inclusion of a function prototype does not cause any error. Well, it is because the function prototype does not conflict each other.

Now, I have the reason to avoid multiple inclusion of a header file at all times. For example, if in a large program foo.h has been included in bar.h and main.c includes foo.h and bar.h, an error will occur if foo.h contains something (e.g., struct, enum, and typedef) other than just some function-prototypes.
Because struct, enum, and typedef declaration should be put in a header file, no precaution can be taken to make successful multiple inclusion of a header file. Therefore, multiple inclusion of a header file must be avoided at all times with
Code:
#ifndef FOO_H
#define FOO_H 1
...
#endif
construct.

So, the question has been answered.

Thank you very much.


Eus
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 90 ·
4
Replies
90
Views
12K
  • · Replies 49 ·
2
Replies
49
Views
12K