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
AI Thread Summary
The discussion centers on the implications of multiple inclusions of header files in C programming. Initially, the user questioned the necessity of avoiding multiple inclusions when certain precautions are taken, noting that their code compiled successfully with a single header file included multiple times. However, it was clarified that while function prototypes can be included without issue, including structures, enums, or typedefs multiple times can lead to compilation errors due to redefinition.The conversation highlighted the importance of using include guards (e.g., #ifndef, #define, #endif) to prevent issues associated with multiple inclusions. It was emphasized that these guards are essential for maintaining code integrity, especially in larger programs where headers may be included in multiple source files. Ultimately, the consensus is that multiple inclusion of header files should be avoided to prevent potential conflicts and errors, reinforcing the necessity of proper header file management in C programming.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top