简体   繁体   English

在多个文件中使用时,没有参数的#pragma hdrstop会怎么做?

[英]What does #pragma hdrstop without parameter do when used in multiple files?

What is the practical value ("what does it do") of putting #pragma hdrstop (no filename parameter) in a couple of source (cpp) files? #pragma hdrstop (无文件名参数)放在几个源(cpp)文件中的实用价值(“它做了什么”)是什么?

Note that the MSDN docs are (once again) as clear as mud to me. 请注意, MSDN文档 (再一次)对我来说一样清楚。

Edit/Note: I'm asking this, because this answer and the article it links to seem to recommend that. 编辑/注意:我问这个,因为这个答案和它链接的文章似乎建议。 But I do not understand what benefit it has to have a separate pch file for each compilation unit. 但我不明白为每个编译单元分别有一个pch文件有什么好处。

The answer to the original question is that the purpose of having #pragma hdrstop in a file with neither /Yc or /Yu set is that it's ignored and so you can set up a build configuration which builds without precompiled headers and other build configurations that build WITH precompiled headers and you don't need to change the code or the headers that are included at all to do so. 原始问题的答案是,在没有设置/ Yc或/ Yu的文件中使用#pragma hdrstop的目的是忽略它,因此您可以设置构建配置,该构建配置在没有预编译头和其他构建配置的情况下构建使用预编译的头文件,您无需更改所包含的代码或标头。

More detail... 更多详情...

The notes on MSDN say that "The hdrstop pragma gives you additional control over precompilation file names and over the location at which the compilation state is saved." MSDN上的注释说“hdrstop pragma可以让你对预编译文件名和保存编译状态的位置进行额外的控制。” which is true, but it's not especially obvious exactly how useful that can be... 这是真的,但它并不是特别明显有多么有用......

In a nutshell. 简而言之。

  • Putting #pragma hdrstop in a source file that isn't compiled with /Yc or /Yu has no effect at all. 将#pragma hdrstop放入未使用/ Yc或/ Yu编译的源文件中根本没有任何效果。
  • If you have /Yu set for the file then hdrstop tells the compiler to throw away everything before the line on which hdrstop appears and insert the precompiled header instead. 如果您为文件设置了/ Yu,那么hdrstop会告诉编译器在hdrstop出现的行之前丢弃所有内容,而是插入预编译的头。
  • If /Yc is set for the file then hdrstop means save all of the compiled state for everything up to the line on which hdrstop appears as the precompiled header. 如果为文件设置了/ Yc,那么hdrstop意味着保存所有编译状态,直到hdrstop显示为预编译头的行。

The trick is using /Yc and /Yu without the optional header file name; 诀窍是使用/ Yc和/ Yu而没有可选的头文件名; just check the 'use' or 'create' radio button and leave the 'through header' edit box blank (or edit the project file...). 只需选中“使用”或“创建”单选按钮,并将“直通标题”编辑框留空(或编辑项目文件...)。

So you have 1 file, possibly called PrecompiledHeader.cpp which includes the headers that you want to include in the precompiled header and which has #pragma hdrstop at the end of the list of include files. 所以你有一个文件,可能叫做PrecompiledHeader.cpp,它包含你想要包含在预编译头文件中的头文件,并且在包含文件列表的末尾有#pragma hdrstop。 This ONE file is compiled with /Yc. 这个ONE文件是用/ Yc编译的。

You then have all your other cpp files with #pragma hdrstop after the include files that are in your precompiled header. 然后,在预编译头文件中包含的文件之后,将所有其他cpp文件与#pragma hdrstop一起使用。 These files are all compiled with /Yu. 这些文件都是用/ Yu编译的。

This results in PrecompiledHeader.cpp building your (in this example) single pch file and all of the other files using that single pch file. 这导致PrecompiledHeader.cpp构建您的(在此示例中)单个pch文件以及使用该单个pch文件的所有其他文件。

The advantage in doing this is that NONE of your files need to include a 'global' precompiled header building header file - so no stdafx.h or whatever. 这样做的好处是你的文件中没有一个需要包含一个'全局'预编译的头部构建头文件 - 所以没有stdafx.h或其他什么。 This means that you can set up a build configuration which builds WITHOUT precompiled headers where all of the #pragma hdrstop lines are simply ignored. 这意味着您可以设置构建配置,该构建配置构建没有预编译的头文件,其中所有#pragma hdrstop行都被忽略。

This is "good" because it means that you can have a single 'no precomp' build configuration which lets you develop quickly (you can change a single header and NOT force the world to rebuild) and other "normal" configurations that DO use precompiled headers. 这是“好的”,因为这意味着你可以拥有一个'no precomp'构建配置,可以让你快速开发(你可以改变一个头而不强迫世界重建)和其他使用预编译的“普通”配置头。

All the code before #pragma hdrstop will be part of a precompiled header. #pragma hdrstop之前的所有代码都将是预编译头文件的一部分。 If no filename parameter is given, the name of the header will be the base name of the source file with a .PCH extension, as mentioned in the documentation : 如果没有给出filename参数,则标题的名称将是具有.PCH扩展名的源文件的基本名称,如文档所述

The name of the precompiled header file is determined according to the following rules, in order of precedence: 预编译头文件的名称根据以下规则按优先顺序确定:

  1. The argument to the /Fp compiler option / Fp编译器选项的参数

  2. The filename argument to #pragma hdrstop #pragma hdrstop的filename参数

  3. The base name of the source file with a .PCH extension 扩展名为.PCH的源文件的基本名称

So, if you have that on a file named blah.cpp it will produce a file named blah.pch , IFF compiled with /Yc (which only one file should have set). 所以,如果你在一个名为blah.cpp的文件上有它,它将产生一个名为blah.pch的文件,用/Yc编译的IFF(只有一个文件应该设置)。

If not using #pragma hdrstop in your C/C++ project, you muse have a dedicated .cpp file as the source file for the .pch created. 如果在C / C ++项目中没有使用#pragma hdrstop ,那么你可以使用专用的.cpp文件作为.pch创建的源文件。

If using #pragma hdrstop in a .cpp file with which you wish to create .pch file, the .cpp file can have other useful contents after the line of #pragma hdrstop . 如果在希望创建.pch文件的.cpp文件中使用#pragma hdrstop ,则.cpp文件可以在#pragma hdrstop行之后包含其他有用的内容。 When you firstly compile this .cpp file with /Yc compilation option, you get the .pch file and the .obj file. 首次使用/ Yc编译选项编译此.cpp文件时,将获得.pch文件和.obj文件。 When You alter the .cpp file and recompile it with /Yu compilation option, the compiler replaces the contents befor the line of #pragma hdrstop with the .pch file and recompile the part after the line of #pragma hdrstop , create a new .obj file, saving compilation time. 当您更改.cpp文件并使用/ Yu编译选项重新编译它时,编译器将#pragma hdrstop行的内容替换为.pch文件,并在#pragma hdrstop行之后重新编译该部分,创建一个新的.obj文件,节省编译时间。 It is very useful that your project has the only one source file. 您的项目只有一个源文件非常有用。

For source files only using precompile head files, the #pragma hdrstop is a landmark for the compiler which part should be replaced by the .pch file, so you needn't indicate the header file name with /Yu compilation option. 对于仅使用预编译头文件的源文件, #pragma hdrstop是编译器的一个里程碑,哪个部分应该被.pch文件替换,因此您不需要使用/ Yu编译选项指定头文件名。

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

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