-2

Suppose #include <something.h> is contained in A.cpp and B.cpp

And If I run some function "A" in A.cpp and function "B" in B.cpp in the main,

Is "something.h" compiled twice? or once?

rici
  • 234,347
  • 28
  • 237
  • 341
mathcom
  • 113
  • 1
  • 7
  • 1
    The code in `something.h` is compiled twice in this instance, yes. This sounds like an XY problem - do you have some larger issue with this? – M.M Sep 15 '17 at 05:46

2 Answers2

0

Wrong question. What is compiled is technically not a source file, but a translation unit.

Read more several books on C++, and some reference site.

Read also more about the various compilation phases, which includes preprocessing.

So, assuming your compiler is GCC on Linux, you would compile the A.cpp source file (also the headers used in it, so a translation unit) with

 g++ -Wall -Wextra -g -c A.cpp -o A.o

where -Wall asks for almost all warnings, -Wextra asks for more of them, -g asks for debug info (in DWARF format on Linux), -c asks for only compilation, -o A.o asks the object file to be A.o

You could ask the compiler to show every included header with the -H option.

You could get the preprocessed form using

g++ -Wall -Wextra -g -C -E A.cpp > A.ii

then you can look with a pager (or an editor) into the generated A.ii file.

Of course preprocessing would include the preprocessed form of a header at the point it is #include-d

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

Since it's an header, it's not "compiled". It's included as part of your code.

And to avoid some awkward situation, you have to protect your header like this :

#ifndef SOMETHING_H_
#define SOMETHING_H_

/* your code goes here */

#endif /* something.h */
cydef
  • 417
  • 3
  • 10