如何在根目录下包含stdafx.h? [英] How to include the stdafx.h from the root directory?

查看:177
本文介绍了如何在根目录下包含stdafx.h?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS中的显示所有文件选项,我添加了一个文件夹,并在该文件夹中创建了一个新类。因为我使用预编译头文件,我还需要包括相对于新类文件的根目录中的stdafx.h。



在我的cpp文件中,我有

  #include..\stdafx.h

但我得到以下错误:



错误C1010:寻找预编译头时意外的文件结尾。你忘了添加'#includestdafx.h'到你的来源?



我的理解是,..应该指示编译器去一个目录级Visual C ++允许您定义几种设置预编译头文件的方法。

$ c



一个文件负责生成PCH文件。该文件通常是项目中的stdafx.cpp文件,除非 #includestdafx.h,它通常没有任何内容。为THAT ONE FILE配置预编译头,从使用切换到创建。这确保了如果PCH的素数头失去同步,stdafx.cpp首先被编译以重新生成PCH数据文件。在Visual Studio中还有其他配置PCH设置的方法,但这是最常见的。



话虽如此,你的问题肯定是令人讨厌的。用于填写PCH系统并在必须匹配您的#include EXTETLY 文本中指定的使用...和创建...设置的文件名。



因此,很有可能您可以通过将..添加到您的项目包含目录并从#include语句中删除..来解决您的问题。您还可以在项目配置级别将其更改为.. \stdafx.h作为通过标头,但如果您有分层次的多个文件夹中的源文件,这可能是一个问题。



哦,如果在你仔细阅读PCH配置设置的时候你不清楚,如果你不想使用PCH的任何特定的源文件(有时候有理由不)可以关闭特定源文件,否则一定要在每个源文件(c / cpp等)的头部总是有#includeyour-pch-include-file.h。



希望你能休息一下。


With "Show all files" option on in VS, i added a folder and created a new class in that folder. Since i'm using precompiled headers i also need to include the stdafx.h that's in the root directory relative to the new class file.

In my cpp file i have

#include "..\stdafx.h"

Yet i get the following error:

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

My understanding is, that the .. should instruct the compiler to go one directory level up ?

解决方案

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". All files will get this setting (thus the configuration at the project) EXCEPT....

One file is responsible for generating the PCH file. That file is typically the stdafx.cpp file in your project, and it typically has nothing in it except #include "stdafx.h". 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 stdafx.cpp is ALWAYS compiled first to regenerate the PCH data file. There are other ways of configuring PCH setting in Visual Studio, but this is the most common.

That being said, your problem is definitely irritating. The filename used to prime the PCH system and specified on both the "Use..." and "Create..." setting above MUST MATCH THE TEXT IN YOUR #include EXACTLY.

Therefore, it is highly likely you can address your problem by adding ".." to your project include directories and removing the ".." from your #include statement. you could also change it at the project-configuration level to be "..\stdafx.h" as the through-header, but that might be a problem if you have source files in multiple folders hierarchically.

Oh, and if it wasn't clear to you while perusing the PCH configuration settings, if you do NOT want to use PCH for any specific source file (and there are reasons not to sometimes) you can turn it OFF for specific source files, otherwise be sure to always have #include "your-pch-include-file.h" at the head of every source file (c/cpp,etc).

Hope you catch a break.

这篇关于如何在根目录下包含stdafx.h?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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