image
Physics Forums Logo
image
image
* Register * Upgrade Blogs Library Staff Rules Mark Forums Read
image
image   image
image

Go Back   Physics Forums > Other Sciences > Computing & Technology > Programming & Comp Sci


Reply

image Why not multiple inclusion of a header file if certain precautions are taken? Share It Thread Tools Search this Thread image
Old Mar7-08, 08:43 AM                  #1
Eus

Eus is Offline:
Posts: 94
Why not multiple inclusion of a header file if certain precautions are taken?

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.

Best regards,
Eus
  Reply With Quote
Old Mar7-08, 09:56 AM                  #2
basePARTICLE

basePARTICLE is Offline:
Posts: 86
I think, the c preprocessor takes the last definition of a meta variable.
  Reply With Quote
Old Mar7-08, 10:42 AM                  #3
jim mcnamara

jim mcnamara is Offline:
Posts: 981
Recognitions:
Science Advisor Science Advisor
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.
  Reply With Quote
Old Mar7-08, 10:45 AM                  #4
jim mcnamara

jim mcnamara is Offline:
Posts: 981
Recognitions:
Science Advisor Science Advisor
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.
  Reply With Quote
Old Mar7-08, 10:51 AM                  #5
D H

PF Mentor

D H is Online:
Posts: 5,622
Originally Posted by Eus View Post
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).

Originally Posted by basePARTICLE View Post
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?
  Reply With Quote
Old Mar8-08, 06:27 AM                  #6
Eus

Eus is Offline:
Posts: 94
Hi Ho!

Originally Posted by D H View Post
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.

Best regards,
Eus
  Reply With Quote
image image
Reply
Thread Tools


Similar Threads for: Why not multiple inclusion of a header file if certain precautions are taken?
Thread Thread Starter Forum Replies Last Post
interactive file naming for naming multiple files in fortran90 dealove Programming & Comp Sci 2 Jun20-08 02:42 PM
Experiment Precautions exec Introductory Physics 0 Nov12-07 09:54 AM
Every possible header file? sweetjones Programming & Comp Sci 1 Oct11-07 02:23 PM
Infection Precautions cronxeh Biology 5 Jun6-05 02:33 PM
Make a Pdf file into an ordinary web file (html file)? sobored Computing & Technology 3 Apr12-05 12:35 PM

Powered by vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. © 2010 Physics Forums
Sciam | physorgPhysorg.com Science News Partner
image
image   image