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?
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?
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
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 */