C++ 如何在项目中实现预编译头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1552058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:24:14  来源:igfitidea点击:

How to implement precompiled headers into your project

c++visual-c++visual-studio-2005includeprecompiled-headers

提问by Chris

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:

我了解预编译头文件背后的目的和原因。但是,在实施它们时有哪些规则?根据我的理解,它是这样的:

  1. Set your project up to use precompiled headers with the YU directive.
  2. Create your stdafx.h file and set that to be your precompiled header.
  3. Include this as the top include statement in each of your .h files.
  1. 将您的项目设置为使用带有 YU 指令的预编译头文件。
  2. 创建您的 stdafx.h 文件并将其设置为您的预编译头文件。
  3. 将此作为每个 .h 文件中的顶部包含语句。

It this correct? Should you exclude the including it in the files that are included within your precompiled header? Currently, I get the following compilation error when following my intuition with this:

这是正确的吗?您是否应该在预编译头文件中包含的文件中排除包含它?目前,按照我的直觉,我收到以下编译错误:

error C2857: '#include' statement specified with the /Ycstdafx.h command-line option was not found in the source file

错误 C2857:在源文件中找不到用 /Ycstdafx.h 命令行选项指定的“#include”语句

The command-line options are as such:

命令行选项如下:

/Od /I "../External/PlatformSDK/Include" /I ".." /I "../External/atlmfc/Include" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /MDd /Yc"stdafx.h" /Fp"....\Output\LudoCore\Debug\LudoCore.pch" /Fo"....\Output\LudoCore\Debug\" /Fd"....\Output\LudoCore\Debug\vc80.pdb" /W4 /WX /nologo /c /ZI /TP /wd4201 /errorReport:prompt

/Od /I "../External/PlatformSDK/Include" /I ".." /I "../External/atlmfc/Include" /D "_D​​EBUG" /D "_UNICODE" /D "UNICODE" /Gm / EHsc /RTC1 /MDd /Yc"stdafx.h" /Fp"....\Output\LudoCore\Debug\LudoCore.pch" /Fo"....\Output\LudoCore\Debug\" /Fd".. ..\Output\LudoCore\Debug\vc80.pdb" /W4 /WX /nologo /c /ZI /TP /wd4201 /errorReport:prompt

回答by ChrisW

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

你的 stdafx.cpp 应该包含 stdafx.h 并使用/Yc"stdafx.h".

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

您的其他 *.cpp 应该包含 stdafx.h 并使用/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:

这是用于创建预编译头文件的 stdafx.cpp 的 Visual Studio 设置的屏幕截图:

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):

以下是相应的命令行选项(它们是只读的,但反映在其他页面上指定的设置;请注意,IDE 在编译器选项中的文件名周围插入双引号字符):

options

选项

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

这是我的 stdafx.cpp 文件中的内容:

// 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

回答by Spencer Rose

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

Visual Studio 不仅可以按项目还可以按源文件存储预编译的头属性。

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.

默认情况下,所有属性都设置为“从父级或项目默认值继承”,它仅显示该属性的父级对象的值,但一旦在子级(在本例中为 .cpp 文件)覆盖,该文件将忽略来自父级属性的更改.

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)
  1. 在选择“所有配置”的项目级别(项目属性,从调试/发布更改配置下拉菜单),将预编译头设置为“使用”
  2. 选择一个 .cpp 文件作为标题的来源(据我所知,哪个无关紧要)
  3. 右键单击并转到属性,然后为预编译头选择“创建”
  4. 最后确保没有其他 .cpp 文件为预编译头属性设置了值(您可以通过粗体文本判断)

回答by Alan

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.

#include "stdafx.h"应该是每个 cpp 文件的第一行。它不应在 .h 文件中使用。除此之外,你是对的。

回答by MSalters

"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.

“stdafx”只是一个约定。这绝不是强制性的。在多项目解决方案中,我使用了其他设置,其中包含针对不同部分的多个预编译头文件。例如,让您的 UI 项目共享一个 PCH,而您的数据库项目共享另一个 PCH 可能很有用。

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.

相关组件是列出可预编译头的 Xh 文件、仅包含 Xh(并且本身不添加任何代码)的 X.cpp 文件,以及通过使用编译器选项编译 X.cpp(因此 Xh)创建的 X.pch 文件/Yc

When you're now compiling Y.cpp file with /Yu"X.pch", the compiler reads and discardseverything 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").

当您现在使用 编译 Y.cpp 文件时/Yu"X.pch",编译器会读取并丢弃直到#include "X.h". 此时,它用存储在 X.pch 中的状态替换其内部状态,但输入流除外(仍为 Y.cpp,文件指针设置为 后的下一行#include "X.h")。