How to use stdafx.h properly?

前端 未结 5 550
借酒劲吻你
借酒劲吻你 2020-12-31 06:56

What is the proper way to use stdafx.h, in terms of separating dependencies?

If I put everything in there, then my compiles ar

相关标签:
5条回答
  • 2020-12-31 07:17

    Personally, I would rather have slower compilation than no idea (visibility) how dependencies are. However, there are limits to everything.

    As each project has its own pre-compiled header file, so I'd just put common-to-all-includes in there. Say if a project uses some boost headers those would fit well there as they will be used by the whole project and you're not changing them. Hence, put rarely/never changing headers in pre-compiled headers such as system or third party stuff.

    For compilation speed, I'd rather rely on as much as possible to forward-declare things, split into smaller libraries and try to see if things can be organized in a way where volatile code is not affecting recompilation of much other code.

    0 讨论(0)
  • 2020-12-31 07:33

    You shouldn't put your own header files in stdafx.h since your code is likely to change and will cause your whole program to recompile.

    I usually put standard headers and other libraries in there, such as all boost includes.

    0 讨论(0)
  • 2020-12-31 07:33

    Conditionally include the PCH's, then define something like "#define _PRE_COMPILE_" This way the original headers are still visible and the code is modular if you so need. An example of this would be to include

    #ifdef _PRE_COMPILE_
    #include "stdafx.h"
    #elif
    #include <iostream>
    #include <cstring>
    #endif
    

    Include this at the beginning of every source file.

    0 讨论(0)
  • 2020-12-31 07:39

    When using precompiled headers, you should make sure your project compiles cleanly even without PCH present (so treat it as optimisation, not a replacement for includes in every TU). Aside from that, don't put absolutely everything in there (and more importantly, don't ever put headers from your own project — PCH should contain only those that don't change or change very rarely — system libraries, external dependencies), but rather try to keep most used/biggest things precompiled.

    0 讨论(0)
  • 2020-12-31 07:43

    Yor compiler might have a "show includes" flag. Turn it on, and look for deeply nested include hierarchies that are repeated. Consider including one of the most shallow include files to your header for each such deep trees.

    0 讨论(0)
提交回复
热议问题