user103214
user103214

Reputation: 3658

Do include guards mean that only one .cpp file gets the header content?

When I include a header file, lets say,

//myheader.h
#ifndef MY_HEADER_H
#define MY_HEADER_H

//....

#endif

into,

//mycpp1.cpp
#include "myheader.h"

What I'm told is, when mycpp1.cpp includes myheader.h, MY_HEADER_H gets defined so any attempt to include it again will result false.

Now, if i want to include it in mycpp2.cpp.

//mpcpp2.cpp
#include "myheader.h"

Would it get included or does it use the same declarations when it was included the first time?

Upvotes: 3

Views: 211

Answers (5)

onemach
onemach

Reputation: 4325

If MY_HEADER_H is just defined in mycpp1.cpp, the header file will be included in mycpp2.cpp

what the trick is really for is like:

header1.h includes header2.h.
So when you include both header1.h and header2.h in your mycpp.cpp, header2.h will be included twice if you did not do the trick.

Upvotes: 3

ch0kee
ch0kee

Reputation: 746

Preprocessor defines are local to the current compilation unit.

Of course there are complex cases, but here is the point:

Try to look at .cpp (source) files as distinct entities. If you don't make really weird things, then if you remove all .cpp files except the one you bother, you can stil compile, because in the compiling stage no need for definitions, you care only the name (declaration) of things.

So, compiling N source files in one run is essentially this:

[ *.H + SOURCE1.CPP ] --> SOURCE1.O
[ *.H + SOURCE2.CPP ] --> SOURCE2.O
...
[ *.H + SOURCEN.CPP ] --> SOURCEN.O

where every line is a distinct compilation unit, which renders the SourceX.CPP and the included headers into an Object file. So we got here N separate thing.

This way, if you don't change the common headers, then you don't have to recompile unmodified source files. Of course, if you modify a source file, you have to recompile that. And finally, if you modify a common header, then you have to recompile every source file that included it. Here I have to mention, that before the compiling phase, all #inlude "filename.ext" line (preprocessor directive) will be replaced with the exact content of the filename.ext file, whatever it is.

Then linking is a different problem, in that stage the goal is to create one single file from that N object files. (I repeat, this is the simple case)

This is linking:

[ SOURCE1.O + SOURCE2.O + ... + SOURCEN.O ]  --> EXECUTABLE.FILE

Imagine the affected the object files as a bag of values and algorithms (function definitions). For example, somewhere in the bag there must be exactly one main function (definition), so the linker will definitely know what to do when you execute the program.

Hope you got it

Guess what happens, if you write a global function's definition into a header file, and then you include it in two separate compilation unit, then you try to link them.

The Answer: linker error - multiple definitions, since you can compile them separately.

Upvotes: 4

Drew Chapin
Drew Chapin

Reputation: 7989

As the other have stated, the header file is included once per .cpp file. But I also wanted to mention that the whole purpose of this:

#ifndef MY_HEADER_H
#define MY_HEADER_H
// ..
#end if

is to prevent this from causing a problem

mycpp1.cpp

#include "myheader.h"
#include "myheader.h"
// ...

another thing I had trouble with when I started was when you include your header file in more than one .cpp file and have global variables defined in it... here's what I do to solve that...

myheader.h

#ifdef MY_HEADER_H
#define MY_HEADER_H

#ifdef GLOBAL_EXTERN
extern int nGlobalInt;
#else
int nGlobalInt = 282;
#endif

#endif

mycpp1.cpp

#include "myheader.h"
// ...

cout << nGlobalInt;

mycpp2.cpp

#define GLOBAL_EXTERN
#include "myheader.h"
// ...

cout << nGlobalInt;

both cpp files will print 282

Upvotes: 1

Gowtham
Gowtham

Reputation: 1475

It will be included only once. The directive MY_HEADER_H would get defined on the first inclusion and all subsequent attempts to #include myheader.h would have no effect.

The preprocessor directives prevail across files. Otherwise, for example, every class declared in myheader.h, would be redefined.

Upvotes: -1

Seth Carnegie
Seth Carnegie

Reputation: 75130

Preprocessor definitions are seperate for each file. So if you #include myheader.h into two seperate .cpp files, it will be included twice, not once. One per .cpp.

Upvotes: 7

Related Questions