如何从根目录包含 stdafx.h?

2021-12-22 00:00:00 visual-c++ c++

在 VS 中启用显示所有文件"选项后,我添加了一个文件夹并在该文件夹中创建了一个新类.由于我使用的是预编译头文件,因此我还需要包含相对于新类文件的根目录中的 stdafx.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.

在我的 cpp 文件中

In my cpp file i have

#include "..stdafx.h"

但我收到以下错误:

错误C1010:查找预编译头时文件意外结束.您是否忘记在源代码中添加#include "stdafx.h""?

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++ 允许您定义几种设置预编译头文件的方法.最常见的是在项目配置级别为所有源文件启用它,在Configuration Properties/C++/Precompiled Headers下,设置Precompiled Header",选择Use".相同的位置,设置预编译头文件",通常是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". All files will get this setting (thus the configuration at the project) EXCEPT....

一个文件负责生成 PCH 文件.该文件通常是您项目中的 stdafx.cpp 文件,除了 #include "stdafx.h" 之外,它通常没有任何内容.为那个文件配置预编译头,从使用"切换到创建".这确保如果 PCH 的主要头文件不同步 stdafx.cpp 总是首先编译以重新生成 PCH 数据文件.在 Visual Studio 中还有其他配置 PCH 设置的方法,但这是最常见的.

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.

话虽如此,您的问题肯定令人恼火.用于启动 PCH 系统并在上面的使用..."和创建..."设置中指定的文件名必须与 #include 中的文本完全匹配.

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.

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

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.

哦,如果您在细读 PCH 配置设置时不清楚,如果您不想将 PCH 用于任何特定的源文件(有时也有理由不这样做),您可以将其关闭以用于特定的源文件,否则请确保在每个源文件(c/cpp 等)的开头始终使用 #include "your-pch-include-file.h".

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).

希望你休息一下.

相关文章