C++ 预编译头文件和 Visual Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6650709/
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-28 20:28:37  来源:igfitidea点击:

Precompiled header and Visual Studio

c++visual-studiovisual-c++precompiled-headers

提问by Mujo Osmanovic

Is there a way to set Visual Studio solution parameters so it just create precompiled headers without building whole solution. Specifically, it is a huge c++ solution with many projects in itself.

有没有办法设置 Visual Studio 解决方案参数,以便它只创建预编译头而不构建整个解决方案。具体来说,它是一个巨大的 C++ 解决方案,本身就有许多项目。

Thank you.

谢谢你。

回答by Benjamin Lindley

Only select the pch creator source file (usually stdafx.cpp), and compile that (Ctrl-F7, or right-click it and select 'Compile')

只选择 pch creator 源文件(通常是 stdafx.cpp),然后编译它(Ctrl-F7,或右键单击它并选择“编译”)

More info since it doesn't seem to be working for you:

更多信息,因为它似乎对您不起作用:

In every project that uses a precompiled header, there is one source file that is used to create the pch file, and the rest only usethe pch file. This source file usually only consists of one line:

在每个使用预编译头的项目中,都有一个源文件用于创建 pch 文件,其余的仅使用pch 文件。这个源文件通常只包含一行:

#include "StdAfx.h"

"Stdafx.h" is the default precompiled header file name in Visual C++, it could be something else for you. If you compile "StdAfx.cpp" by itself, that generates a file with the name "Your_Project_Name.pch" (again, that's only the default). You should see it in the intermediate directory, the same one where all the obj files are. This is the precompiled header. If you did like I said, and selected 'Compile' and not 'Build', then no other files will be compiled, and no linking will take place.

“Stdafx.h”是 Visual C++ 中默认的预编译头文件名,它可能是其他名称。如果您自己编译“StdAfx.cpp”,则会生成一个名为“Your_Project_Name.pch”的文件(同样,这只是默认值)。您应该在中间目录中看到它,与所有 obj 文件所在的目录相同。这是预编译的头文件。如果你像我说的那样,选择了“编译”而不是“构建”,那么不会编译其他文件,也不会发生链接。

If that still does not solve your problem, then I have no idea what you are asking.

如果那仍然不能解决您的问题,那么我不知道您在问什么。

回答by Csimbi

Here's an articledescribing the benefits and how to set it up. If you have larger projects, it is definitely worth the few clicks and the extra disk space.

是一篇描述好处以及如何设置它的文章。如果您有更大的项目,那么点击几下和额外的磁盘空间绝对值得。

The article says you need to add stdafx.h to all sources and headers; it's not correct. It's sufficient to add to sources - make sure it's the first line though as everything before it will be ignored.

文章说你需要将 stdafx.h 添加到所有源和头文件中;这是不正确的。添加到源代码就足够了 - 确保它是第一行,因为它之前的所有内容都将被忽略。

The article does not mention it, but you'll be getting errors from sources that do not include the stdafx.h. You have a choice to resolve this error: you either add it, or exclude the source(s) from the precompilation process. To exclude source files: - select source file(s) in the solution explorer; yes you can select more at once, - right click on the highlighted source file, - click properties from the pop-up menu, - select 'All configurations' from the combo-box on top, - under C-C++ configuration click 'Precompiled headers', - on the right-hand side select 'Not using precompiled headers', - click apply, - click ok.

这篇文章没有提到它,但是你会从不包含 stdafx.h 的来源中得到错误。您可以选择解决此错误:添加它或从预编译过程中排除源。排除源文件: - 在解决方案资源管理器中选择源文件;是的,您可以一次选择更多,- 右键单击​​突出显示的源文件,- 从弹出菜单中单击属性,- 从顶部的组合框中选择“所有配置”,- 在 C-C++ 配置下单击“预编译”标头”,-在右侧选择“不使用预编译标头”,-单击应用,-单击确定。

Enjoy your new builds in a few seconds from here on (the first build will take longer).

从现在开始,在几秒钟内享受您的新构建(第一个构建需要更长的时间)。

回答by PeeWee2201

If you right-click any Cpp files except stdafx.cpp from your project and set Excluded from build to Yes, it will only generate the precompiled header.

如果您右键单击项目中除 stdafx.cpp 之外的任何 Cpp 文件并将排除从构建设置为是,它只会生成预编译头。

You can achieve the same result through the command line or if you create new project containing only your stdafx.cpp

您可以通过命令行实现相同的结果,或者如果您创建仅包含 stdafx.cpp 的新项目