什么是"stdafx.h"?用于Visual Studio? [英] What is "stdafx.h" used for in Visual Studio?

查看:204
本文介绍了什么是"stdafx.h"?用于Visual Studio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010中启动项目时,会自动生成一个名为stdafx.h的文件.我需要制作一个跨平台的C ++库,所以我不能/不能使用此头文件.

A file named stdafx.h is automatically generated when I start a project in Visual Studio 2010. I need to make a cross-platform C++ library, so I don't/can't use this header file.

stdafx.h的作用是什么?我可以删除此头文件吗?

What is stdafx.h used for? Is it OK that I just remove this header file?

推荐答案

所有C ++编译器都存在一个严重的性能问题.编译C ++代码是一个漫长而缓慢的过程.

All C++ compilers have one serious performance problem to deal with. Compiling C++ code is a long, slow process.

包含在C ++文件顶部的编译头是一个非常漫长而缓慢的过程.编译构成Windows API和其他大型API库的一部分的巨大标头结构是一个很长,很慢的过程,非常非常.不得不为每个Cpp源文件一遍又一遍地重复操作,这真是丧钟.

Compiling headers included on top of C++ files is a very long, slow process. Compiling the huge header structures that form part of Windows API and other large API libraries is a very, very long, slow process. To have to do it over, and over, and over for every single Cpp source file is a death knell.

这不是Windows独有的,而是所有必须针对大型API(如Windows)进行编译的编译器都面临的一个老问题.

This is not unique to Windows but an old problem faced by all compilers that have to compile against a large API like Windows.

Microsoft编译器可以通过称为 precompiled headers 的简单技巧来解决此问题.诀窍非常巧妙:尽管每个CPP文件都可以潜在地和合法地为每个Cpp文件顶部包含的头文件链赋予不同的含义(例如,在包含之前预先#define定义不同的宏,或者(以不同的顺序包含标题),通常情况并非如此.在大多数情况下,我们会包含数十个或数百个包含的文件,但是对于您的应用程序中正在编译的所有Cpp文件,它们都具有相同的含义.

The Microsoft compiler can ameliorate this problem with a simple trick called precompiled headers. The trick is pretty slick: although every CPP file can potentially and legally give a sligthly different meaning to the chain of header files included on top of each Cpp file (by things like having different macros #define'd in advance of the includes, or by including the headers in different order), that is most often not the case. Most of the time, we have dozens or hundreds of included files, but they all are intended to have the same meaning for all the Cpp files being compiled in your application.

如果编译器不必每次都从头开始从头开始编译每个Cpp文件及其数十个包含项,则可以节省大量时间.

The compiler can make huge time savings if it doesn't have to start to compile every Cpp file plus its dozens of includes literally from scratch every time.

该技巧包括指定一个特殊的头文件作为所有编译链的起点,即所谓的预编译头"文件,通常出于历史原因,该文件通常名为 stdafx.h

The trick consists of designating a special header file as the starting point of all compilation chains, the so called 'precompiled header' file, which is commonly a file named stdafx.h simply for historical reasons.

只需按照适当的顺序在stdafx.h文件中列出您的API的所有大型头文件,然后在任何有意义的内容之前(以大约唯一允许的是评论).

Simply list all your big huge headers for your APIs in your stdafx.h file, in the appropriate order, and then start each of your CPP files at the very top with an #include "stdafx.h", before any meaningful content (just about the only thing allowed before is comments).

在这种情况下,编译器无需从头开始 ,而是从已经保存的编译stdafx.h中所有内容的结果开始进行编译.

Under those conditions, instead of starting from scratch, the compiler starts compiling from the already saved results of compiling everything in stdafx.h.

我不认为此技巧是Microsoft编译器独有的,也不认为这是原始开发.

I don't believe that this trick is unique to Microsoft compilers, nor do I think it was an original development.

对于Microsoft编译器,控制预编译头的使用的设置由编译器的命令行参数/Yu "stdafx.h"控制.可以想象,使用stdafx.h文件名只是一个约定;您可以根据需要更改名称.

For Microsoft compilers, the setting that controls the use of precompiled headers is controlled by a command line argument to the compiler: /Yu "stdafx.h". As you can imagine, the use of the stdafx.h file name is simply a convention; you can change the name if you so wish.

在Visual Studio 2010中,可以通过在GUI上右键单击CPP项目,选择属性",然后导航到配置属性\ C/C ++ \ Precompiled标头",从GUI控制此设置.对于其他版本的Visual Studio,GUI中的位置将有所不同.

In Visual Studio 2010, this setting is controlled from the GUI via Right-clicking on a CPP Project, selecting 'Properties' and navigating to "Configuration Properties\C/C++\Precompiled Headers". For other versions of Visual Studio, the location in the GUI will be different.

请注意,如果禁用预编译头(或通过不支持它们的工具运行项目),则不会使程序非法;它只是意味着您的工具每次都会从头开始编译所有内容.

Note that if you disable precompiled headers (or run your project through a tool that doesn't support them), it doesn't make your program illegal; it simply means that your tool will compile everything from scratch every time.

如果要创建不依赖Windows的库,则可以轻松注释掉#include或从stdafx.h文件中删除#include.不需要删除文件本身,但是显然,您也可以通过禁用上面的预编译头设置来删除文件.

If you are creating a library with no Windows dependencies, you can easily comment out or remove #includes from the stdafx.h file. There is no need to remove the file per se, but clearly you may do so as well, by disabling the precompile header setting above.

这篇关于什么是"stdafx.h"?用于Visual Studio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆