Suppose there are two header files, header1.h and header2.h, and, let's say, there are three .cpp files - code1, code2, code3 of approximately the same content:


#include "header1.h" #include "header2.h" //some code... 


 #include "header1.h" 


  //some code 

The question is how many object files will be generated and how did you calculate the number of object modules?

    1 answer 1

    3, one for each cpp file. And the count of h files does not matter. Well, except for those cases where h file just has such an extension, but is not a header.

    • Those. Although code1.cpp and code2.cpp include the same header, will they be in different object modules? - PaulD
    • 2
      The key point is just accept the fact that include simply inserts the file in the specified location. It is the usual, mechanical insert without some sort of logic. Then the preprocessor passes and deals with ifdef , define and similar ifdef . Of course, modern compilers can do some analysis and speed up this process, not including the file in vain. To warm up, I recommend to study the output of g++ -E filename.cpp or cl /E filename.cpp - you can see the result of the preprocessor. Sometimes it is very useful if the code has complex macros. - KoVadim
    • 2
      @SoloMio, the compiler does (if it is asked to make object files) strictly one .o from each .cpp .h files are textually added to .cpp at the compilation stage. You can make a boot module (in Windows .exe) from several .cpps bypassing .o And you start the compiler with different flags and combinations of files and see what new files appear. - avp
    • Clearly, thank you all very much, be sure to experiment. - PaulD