Hi Ho!
Originally Posted by D H
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