After building the project, one “instance” of the .h file remains or as many as it was connected? If the latter, does include guard save from this waste of memory?

By include guard I understand:

 # pragma once 


 #ifndef FILENAME_H #define FILENAME_H // код #endif 
  • 3
    - Header files without include guards not needed by anyone. - By itself, the question about "samples" of headers suggests that you are not well versed in the stages of the compiler and linker. That is, roughly speaking, if you understand them, then such a question simply will not arise :) - Costantino Rupert
  • And can you be more specific? I don’t have time to learn the compilation steps. - chevchelios
  • Compilation steps need to be known and understood very well. It is easy and study takes a bit. Half an hour is enough, but without this serious study and use of C ++ is simply impossible. - skegg
  • To understand well, you need to find a good explanation. The search yielded only 2 links, but there is almost no information there either. - chevchelios

2 answers 2

  1. Include guard'y are not needed to save memory, but to prevent errors that may occur when you turn on the same header again in the module file.
  2. Not the header itself (the text of which is simply included by the preprocessor in the module text) can be saved, but what is contained in it. If, in addition to the function declarations, it also contains different constants, function definitions (which is not good, but it occurs) or templates, then they are compiled separately for each object file and then compiled. True, some linkers can find such repeats and eliminate them.
  • Well, it can not be that the compiler developers have not solved the problem of duplicating code. - chevchelios
  • I say, under certain conditions, this problem is solved, but at the linker level. - skegg
  • @mikillskeg some compilers store translated (though I don’t know what form) the headers. To accelerate the translation. True to the question @chevchelios is perpendicular, since the headers in the intermediate format are not duplicated. - alexlz
  • @alexlz, do you mean precomposed headers? - skegg
  • @mikillskeg aha. Although they have nothing to do with the question. So, an attempt to reduce the time to recompile hundreds of thousands of lines ... - alexlz

@chevchelios , the first stage of C / C ++ compilation is the work of the preprocessor.

It reads the specified file with the program, executes directives starting with the # character ( #define , #include , etc.) and makes text macro substitutions (replaces (roughly speaking) the text in the program with the text defined in #define directives)

Those. the preprocessor changes the TEXT of the program, without particularly delving into its meaning. The #include directive says that instead of the line with it, you need to insert the lines of the specified file (recursively).

If you run gcc -E prog.c or g ++ -E prog.cpp, you will see how the text of your program has changed. It is placed in a temporary file (or directly (by pipe) passed to the compiler). It is this text that is processed by the compiler that produces the object module .

Regarding developers and solving the problem of duplication She successfully solved. "Programmer" reported on his mistakes.

  • I understand the first stage) But when all these .cpp files, after being converted into an object module, begin to merge into one file, then I don’t know what the compiler will do with duplication. Since you say that there is no duplication, I will hope that this is so. - chevchelios
  • With duplication what? The .h files contain nothing that creates names in the object files: the bodies of functions, variables, class objects, etc. And if they get there by some kind of misunderstanding, then the linker will not build the program, with diagnostics like "duplication of the name xxx in the zzz module". - user6550
  • With the duplication of what the compiler permits to duplicate, namely the function prototypes, the declaration of classes / structures (I'm not sure here), the declaration. global objects (extern int foo;) and maybe something else. In all library .h files there is a lot of such "garbage". - chevchelios
  • Advertisements of the same function can be at least four hundred and twenty-three in one place, the compiler can sneeze on it. All this (prototypes, templates, etc.) does not create objects, and in general, it has nothing to do with linking. To speed up the understanding of this fact, try to answer a simple question: why do we need function prototypes at all? - user6550
  • 2
    Just imagine, a girl at the station says: "train number 10 comes to platform 5". And so 20 times in a row. Will appear from this twenty trains? No, only one will come. Here and here. The compiler does not need to delete anything, since declarations affect only the process of building objects, but do not create anything by themselves. - user6550