在标题VS2010中定义的c ++预编译头。编译器找不到 [英] c++ precompiled header defined in a header VS2010. Compiler can't find

查看:208
本文介绍了在标题VS2010中定义的c ++预编译头。编译器找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些像这样的代码,

I've inherited some code that did something like this,

标题:HeaderFile.h

Header: HeaderFile.h

#ifndef HEADERFILE_H
#define HEADERFILE_H

#ifndef HEADERFILE_PCH_H
#include<LibStuff>
#include<LibStuff2>
#include<LibStuff3>
#include<LibStuff4>
#include<LibStuff5>
#endif

#include "FilesInProject"

Class A
{
  //Code

};

#endif

Cpp:HeaderFile.cpp

Cpp: HeaderFile.cpp

#include "HeaderFile_pch.h" //(1)

#include "HeaderFile.h"

//More code

我知道什么是预编译头for和代码在这里做什么(类)。当我把这些文件复制到我的项目,这是我可以更新已弃用的代码,但不影响原来的项目,VS2010 chocks在线(1)。 VS2010说它找不到该文件。

I understand what a precompiled header is for and what the code is doing here (sort of). When I copy these files into my project, this is so I can update deprecated code but not effect the original project, VS2010 chocks on line (1). VS2010 Saying it can't find that file.

我已经在两个项目之间进行,我在设置中找不到任何差异。

I've gone between the two projects and I can't find any differences in settings.

我缺少,为什么可以嵌入 PCH 在这样的头,而不是实际将它们移动到一个文件 HeaderFile_pch.h 。这是一种宏命令吗?

What am I missing and why is it okay to imbed PCH's in headers like this instead of actually moving them to a file called HeaderFile_pch.h. Is this some kind of macro hack?

推荐答案

对于初学者,没有像Headerfile_pch.h这样的文件,

For starters, there is no such file as Headerfile_pch.h either in the samples you provided above nor likely on your local fs.

Visual C ++允许您定义几种设置预编译头文件的方法。最常见的是在项目配置级别为所有源文件启用它,在配置属性/ C ++ /预编译头下,设置预编译头,选择使用。相同的位置,设置预编译头文件,通常是stdafx.h,但可以是你选择的任何东西。所有文件将获得此设置(因此在项目级别的配置)EXCEPT ....

Visual C++ allows you to define several ways of setting up precompiled header files. The most common is to enable it for ALL source files at the project configuration level, Under Configuration Properties/C++/Precompiled Headers, setting "Precompiled Header", select "Use". The same location, setting "Precompiled Header File", is usually "stdafx.h" but can be anything you choose. All files will get this setting (thus the configuration at the project level) EXCEPT....

一个文件负责生成PCH文件。该文件通常是项目中的stdafx.cpp文件,但同样,它可以是您想要的任何单一源。大多数只是设置一个虚拟的cpp文件,其中有一件事:#includemyheader.h(duh)..配置预编译头为THAT ONE文件,从使用切换到创建。这确保了如果PCH的主标题不同步,则首先重新编译源文件以在其它的被启动之前重新生成PCH数据文件。

One file is responsible for generating the PCH file. That file is typically the stdafx.cpp file in your project, but again, it can be whatever single source you desire. Most just setup a dummy cpp file that has one thing in it: #include "myheader.h" (duh).. Configuring Precompiled Headers for THAT ONE FILE, switch from "Use" to "Create". This ensures that if the prime-header for PCH gets out of synch that source file is recompiled first to regenerate the PCH data file before the others are kicked off.

这是绝对必须的:包括标题(myheader.h或任何你调用它)必须是第一包括任何正在编译的源文件,正在参与使用pch包括(希望显然)您指定为生成器(在前一段中标记为创建的文件)的文件。

The one attribute of this that is absolutely mandatory: that include header (myheader.h or whatever you're calling it) must be the first include in any source file you're compiling that is participating in using pch including (hopefully obviously) the file you specified as the "generator" (the one marked as "Create" in the prior paragraph).

最后,如果由于构建环境中的异常情况导致问题,您可以 逐个文件地禁用pch

Lastly, you can disable pch on a file-by-file basis if this is a problem due to unusual conditions in your build environment (i.e. 3rd party headers that do stupid things).

在Visual Studio中还有其他配置PCH设置的方法,但这是通过 far 最常见。

There are other ways of configuring PCH setting in Visual Studio, but this is by far the most common.

这篇关于在标题VS2010中定义的c ++预编译头。编译器找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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