预编译头 [英] Precompiled Headers

查看:35
本文介绍了预编译头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Visual C++ 6 的示例项目(不是我的).我正在尝试将其转换为 Visual Studio 2008.

I have a sample project (not mine) which is in Visual C++ 6. I'm trying to convert it to Visual Studio 2008.

旧项目使用的是预编译头文件.现在的问题是:

The older project is using precompiled headers. Now the questions are:

  1. 什么是预编译头?

  1. What are precompiled headers?

因为旧项目使用的是预编译头文件.我还将在 Visual Studio 2008(新项目)中使用它们.但是我收到错误消息,说您是否忘记包含 stdafx.h",为了解决这个问题,我在每个源文件中都包含了stdafx.h".那工作得很好.但是旧项目没有在每个文件中都包含stdafx.h"?那么我如何选择退出在每个源文件中包含stdafx.h".因为不是每个源文件都需要在stdafx.h"中定义的包含文件,只有少数需要.这是怎么做到的?

Since the older project is using precompiled headers. I'll also use them in Visual Studio 2008 (the new project). But I get errors saying that "Did you forget to include stdafx.h", to remedy the problem, I include "stdafx.h" in every source file. That worked perfectly. But the older project was not including "stdafx.h" in every file? Then how can I opt-out to include "stdafx.h" in each source file. Because not every source file need the include files defined in "stdafx.h", only few does. How is that done?

我如何使用预编译头文件排除某些文件?

推荐答案

什么是预编译头?

C++ 源文件通常包含来自外部库的头文件.在 Windows 中,您包含 windows.h.这些头文件可能非常大,需要一些时间来处理.每次编译 C++ 文件时,编译器都必须从这些头文件中读取和处理数千行.但是外部库不会改变,如果您只处理一次这些文件并保存结果,您可以节省大量时间.

Often C++ source files include headers from external libraries. In Windows you include windows.h. These header files can be very large and they take some time to process. Each time you compile a C++ file the compiler has to read and process thousands of lines from these header files. But external libraries don't change and you can save a lot of time if you only process these files once and save the result.

预编译头文件只是一堆头文件,它们已经被处理成一种中间形式,以后可以被编译器一次又一次地使用.

A precompiled header is simply a bunch of header files that has been processed to an intermediate form that later can be used by the compiler again and again.

Visual C++ 中的预编译头

在 Visual C++ 中,习惯上在 stdafx.h#include 所有不变的头文件.然后指示编译器在编译 stdafx.cpp 时创建预编译头文件 stdafx.pch,它只包含 stdafx.h.如果你想在另一个 .cpp 文件中使用预编译头文件,你必须包含 stdafx.h 作为第一个包含文件,并指示编译器使用 stdafx.pch 用于预编译头.

In Visual C++ it is customary to #include all your non-changing header files in stdafx.h. You then instruct the compiler to create the precompiled header stdafx.pch while compiling stdafx.cpp which does nothing but include stdafx.h. If you want to use the precompiled header in another .cpp file you have to include stdafx.h as the first include file and the instruct the compiler to use stdafx.pch for your precompiled header.

如果您收到关于不包含 stdafx.h 的错误,您只需指示编译器不要为该特定源文件使用预编译头.(或者您可以包含 stdafx.h.)

If you get an error about not including stdafx.h you simply have to instruct the compiler to not use a precompiled header for that particular source file. (Or you can include stdafx.h.)

单个源文件的预编译头设置

Visual C++ 允许您控制整个项目和单个文件的编译器设置.要访问单个属性,请在解决方案资源管理器中选择源文件,右键单击它并从上下文菜单中选择属性.预编译头的选项可以在 Configuration Properties => C/C++ => Precompiled Headers 中找到.如果您修改这些设置,您通常会希望对所有配置(例如 DebugRelease)执行此操作.

Visual C++ allows you to control the compiler settings for the entire project and for individual files. To access individual properties you select the source file in the solution explorer, right click it and select Properties from the context menu. The options for precompiled headers are found at Configuration Properties => C/C++ => Precompiled Headers. If you modify these settings you will often want to do that for all configurations (e.g. Debug and Release).

当您使用预编译头文件时,您将为整个项目设置一个设置,指示编译器使用 stdafx.pch 作为预编译头文件.stdafx.cpp 将有一个单独的设置,指示编译器生成 stdafx.pch,如果您有一些不包含 stdafx 的源文件.h 您可以对这些设置单独的设置以不使用预编译头.

When you are using precompiled headers you will have a setting for the entire project that instructs the compiler to use stdafx.pch for the precompiled header. The stdafx.cpp will have an individual settings that instructs the compiler to generate stdafx.pch, and if you have some source files that doesn't include stdafx.h you can set individual settings on these to not use precompiled headers.

这篇关于预编译头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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