Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of #ifndef FILENAME....#endif in header file

Tags:

c++

c

I know it to prevent multiple inclusion of header file. But suppose I ensure that I will include this file in only one .cpp file only once. Are there still scenarios in which I would require this safe-guard?

like image 260
deeJ Avatar asked Feb 04 '10 08:02

deeJ


People also ask

What is the purpose of meaning?

So, meaning means "the effect that something has on one's preferences," and purpose means "the use that something has on our attaining one's preferences." They're closely linked.

Do you say for purpose of or for purposes of?

Both are correct. “For the purpose of” is followed by a gerund, and the phrase can typically be replaced with “to” followed by an infinitive. “This spoon can be used for the purpose of stirring ingredients.”

What is a good sentence for purpose?

Examples of purpose in a SentenceThe loans are small but they serve a good purpose. Sometimes his life seemed to lack purpose or meaning.

Can you start a sentence with for the purpose of?

"for the purpose of" Example SentencesThis meeting has been called for the purpose of clarifying several important issues. For the purpose of improving security, the airport increased the number of guards. For the purpose of this discussion, gross revenue is a more important consideration than profits.


2 Answers

No, that's the only purpose of the include guards, but using them should be a no-brainer: doing it requires little time and potentially saves a lot.

like image 83
Paolo Tedesco Avatar answered Nov 08 '22 16:11

Paolo Tedesco


You can guarantee that your code only includes it once, but can you guarantee that anyone's code will include it once?

Furthermore, imagine this:

// a.h
typedef struct { int x; int y; } type1;

// b.h
#include "a.h"
typedef struct { type1 old; int z; } type2;

// main.c
#include "a.h"
#include "b.h"

Oh, no! Our main.c only included each once, but b.h includes a.h, so we got a.h twice, despite our best efforts.

Now imagine this hidden behind three or more layers of #includes and it's a minor internal-use-only header that gets included twice and it's a problem because one of the headers #undefed a macro that it defined but the second header #defined it again and broke some code and it takes a couple hours to figure out why there are conflicting definitions of things.

like image 24
Chris Lutz Avatar answered Nov 08 '22 17:11

Chris Lutz