什么是“stdafx.h"?在 Visual Studio 中使用? [英] What is "stdafx.h" used for in Visual Studio?

查看:44
本文介绍了什么是“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 独有的问题,而是所有编译器都面临的老问题,这些编译器必须针对像 Windows 这样的大型 API 进行编译.

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 编译器可以通过一个称为预编译头的简单技巧来改善这个问题.技巧非常巧妙:尽管每个 CPP 文件都可能在法律上赋予每个 Cpp 文件顶部包含的头文件链略有不同的含义(例如在包含之前#define'd 不同的宏,或通过以不同的顺序包含标题),通常情况并非如此.大多数情况下,我们有数十或数百个包含文件,但它们都旨在对您的应用程序中正在编译的所有 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 的所有大型头文件,然后在每个 CPP 文件的最顶部使用 #include "stdafx.h",在任何有意义的内容之前(几乎唯一允许的内容是评论).

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.

我不认为这个技巧是微软编译器独有的,也不认为它是一个原创的开发.

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 中,通过右键单击 CPP 项目,选择属性"并导航到配置属性C/C++预编译头文件",可以从 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 PropertiesC/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 依赖项的库,您可以轻松地从 stdafx.h 文件中注释掉或删除 #includes.不需要删除文件本身,但显然您也可以通过禁用上面的预编译头设置来删除.

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天全站免登陆