简体   繁体   English

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

[英]How to implement precompiled headers into your project

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. 将您的项目设置为使用带有YU指令的预编译头文件。
  2. Create your stdafx.h file and set that to be your precompiled header. 创建您的stdafx.h文件,并将其设置为您的预编译头。
  3. Include this as the top include statement in each of your .h files. 将此作为每个.h文件中的头一个include语句。

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“ _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

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设置的屏幕截图:

创建预编译头

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在编译器选项中的文件名周围插入双引号字符):

选项

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

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) 选择一个.cpp文件作为标头的来源(据我所知,哪一个都没有关系)
  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) 最后,确保没有其他.cpp文件为预编译的标头属性设置值(您可以通过粗体字看出来)

Your #include "stdafx.h" should be the first line of each cpp file. 您的#include "stdafx.h"应该是每个cpp文件的第一行。 It shouldn't be used in the .h files. .h文件中不应使用它。 Other than that you're about right. 除此之外,您是正确的。

"stdafx" is just a convention. “ stdafx”只是一个约定。 It's in no way mandatory. 绝不是强制性的。 In a multi-project solution, I've used other setups with multiple pre-compiled headers for different parts. 在多项目解决方案中,我使用了其他设置以及针对不同部分的多个预编译头文件。 Eg it may be useful to have one PCH shared by your UI projects, and another one for your database projects. 例如,一个UI项目共享一个PCH,而另一个数据库项目共享一个PCH可能会很有用。

The relevant components are the Xh file listing precompilable headers, the X.cpp file that includes only Xh (and adds no code itself), and the X.pch file created by compiling X.cpp (and thus Xh) with compiler option /Yc . 相关组件包括列出预编译头的Xh文件,仅包含Xh的X.cpp文件(本身不添加代码)以及通过使用编译器选项/Yc编译X.cpp(因此也包括Xh)创建的X.pch文件。 。

When you're now compiling Y.cpp file with /Yu"X.pch" , the compiler reads and discards everything up to #include "Xh" . 现在,使用/Yu"X.pch"编译Y.cpp文件时,编译器将读取并舍弃所有包含#include "Xh" 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 "Xh" ). 那时,它用存储在X.pch中的状态替换其内部状态,除了输入流(保持Y.cpp,文件指针设置为#include "Xh"之后的下一行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM