Should we include the standard library header files ( string , algorithm , etc.) in the precompiled header file stdafx.h , because they do not change?

It is embarrassing to write #include "stdafx.h" in each .cpp file. Used by Visual Studio 2015.

  • Why is it necessary? Nobody forces, by and large ... Yes, they give you such a blank, but if you write without it, in principle, nothing terrible will happen ... - Harry
  • @Harry I don’t know if you don’t write the first line #include "stdafx.h in all .cpp files, then I get an error in VS15 , where something like" maybe you forgot to write ... " - joch
  • How to prove to you? Screenshots if I post somewhere - will this be enough for you? :) - Harry
  • @Harry here, for example, clearly said about it habrahabr.ru/company/pvs-studio/blog/227521 - joch

2 answers 2

If you decide to use Precompiled Headers (to speed up the build), then the standard library headers need to be included in stdafx.h because for this, all the fuss is started up, so that the compiler once it has been parsed, and then for all other files already used ( precompiled files. Correspondingly, the files that are not changeable are just the ones that need to be included in it in order not to cause the reassembly of these precompiled headers once again.

The mechanism of precompiled headers is quite flexible. You can disable it completely, or for individual files. And also for one group of files to use one precompiled header, for another group another. Well, the name stdafx.h although by default, but not fixed, can be changed to any other.

    If I understand your question correctly, then I do not recommend including header files inside the <stdafx.h> header.

    Firstly, it makes the code less readable, since it sows doubt about reading your code, whether you included all the headers correctly, or if the program compiles only because in some implementation of the standard library another standard header was included in the standard header.

    Secondly, such code will not be portable to platforms where the <stdafx.h> header is <stdafx.h> . If you do not include header files in it, then you will simply have to comment it out or delete it. That is, the work is simplified. And no one will ask what the title is.

    • 3
      On which platforms can the stdafx.h header be missing? In the sense that it is a user file, not a standard one, i.e. goes along with the rest of your code files. - Vladimir Gamalyan 4:02
    • @VladimirGamalian Questions arise that he means. And most importantly, absolutely not visible what standard headers are used. Moreover, different translation units may use different headers, and cluttering them with unnecessary headers leads to an increase in compile time on those platforms where the meaning of the Microsoft header is not known. - Vlad from Moscow
    • I agree that it is not visible until you open stdafx.h yourself. On the other hand, gcc can also work with precompiled headers (although it seems not as efficient as a studio one). Those. It is not that exclusive from Microsoft. - Vladimir Gamalyan 4:08
    • @VladimirGamalian Exclusive from Microsoft - this is the title. - Vlad from Moscow
    • I do not understand the account of the exclusive. You might as well use the precompiled headers mechanism in other compilers (intel, gcc, borland). And even use the same name ( stdafx.h ), it is hardly patented. - Vladimir Gamalyan