How to implement precompiled headers into your project

后端 未结 4 1383
鱼传尺愫
鱼传尺愫 2021-01-30 07:19

I understand the purpose and reasoning behind precompiled headers. However, what are the rules when implementing them? From my understanding, it goes something like this:

相关标签:
4条回答
  • 2021-01-30 07:47

    "stdafx" is just a convention. It's in no way mandatory. In a multi-project solution, I've used other setups with multiple pre-compiled headers for different parts. E.g. it may be useful to have one PCH shared by your UI projects, and another one for your database projects.

    The relevant components are the X.h file listing precompilable headers, the X.cpp file that includes only X.h (and adds no code itself), and the X.pch file created by compiling X.cpp (and thus X.h) with compiler option /Yc.

    When you're now compiling Y.cpp file with /Yu"X.pch", the compiler reads and discards everything up to #include "X.h". At that point it replaces its internal state with the state stored in X.pch, except for the input stream (remains Y.cpp, with the file pointer set to the next line after #include "X.h").

    0 讨论(0)
  • 2021-01-30 07:59

    Visual studio can store pre-compiled header properties not just by project but by source file.

    By default all properties are set to "inherit from parent or project defaults" which just shows the parent object's value for that property, but once overriden at a child level (in this case a .cpp file) the file ignores changes from the parent property.

    The configuration you want is:

    1. At project level with "All Configurations" selected (project properties, change configuration drop down from debug/release), Set your pre-compiled header to "Use"
    2. Pick a .cpp file to be the source of the header (as far as I know doesn't matter which one)
    3. Right click and goto properties, and select "Create" for precompiled header
    4. Finally make sure that no other .cpp files have values set for the precompiled header property (you can tell by the bold text)
    0 讨论(0)
  • 2021-01-30 08:05

    Your #include "stdafx.h" should be the first line of each cpp file. It shouldn't be used in the .h files. Other than that you're about right.

    0 讨论(0)
  • 2021-01-30 08:09

    You stdafx.cpp should include stdafx.h and be built using /Yc"stdafx.h".

    Your other *.cpp should be include stdafx.h and be built using /Yu"stdafx.h".

    Note the double-quote characters used in the compiler options!

    Here's a screenshot of the Visual Studio settings for stdafx.cpp to create a precompiled header:

    create precompiled header

    Here are the corresponding command-line options (which are read-only but reflect the settings specified on other pages; note that the IDE inserts double-quote characters around the filename, in the compiler option):

    options

    This is what's in my stdafx.cpp file:

    // stdafx.cpp : source file that includes just the standard includes
    // CallWinsock.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information
    
    #include "stdafx.h"
    
    // TODO: reference any additional headers you need in STDAFX.H
    // and not in this file
    
    0 讨论(0)
提交回复
热议问题