visual-studio Visual C++ 预编译头错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1397190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:59:04  来源:igfitidea点击:

Visual C++ Precompiled Headers errors

c++visual-studiovisual-c++headerprecompiled-headers

提问by jameszhao00

Update:

更新:

What are the effects of including stdafx.h in my header files?

在我的头文件中包含 stdafx.h 有什么影响?



I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows.

我在 Linux/Eclipse CDT 中开始了一个 C++ 项目,并将其导入到 Visual C++/Windows 中。

In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h.

在 Visual C++ 中,我开始使用预编译头来加速编译并定义 stdafx.cpp 和 stdafx.h。

Here's my stdafx.h

这是我的 stdafx.h

#pragma once

#include <string>
#include <vector>
#include <map>
...

and my stdafx.cpp

和我的 stdafx.cpp

#include "stdafx.h"

In every .h and .cpp file, I have the following:

在每个 .h 和 .cpp 文件中,我有以下内容:

#pragma once //if in a header file
#include "stdafx.h"

For both release and debug, I have "Create Precompiled Header (/Yc)". It compiled fine in debug mode, but in release mode it keeps reporting

对于发布和调试,我有“创建预编译头 (/Yc)”。它在调试模式下编译得很好,但在发布模式下它不断报告

error LNK2005: ___@@_PchSym_@00@UfhvihUaszlaDUwlxfnvmghUnnlUhixUnnlPeDUnnlPeDUivovzhvUvmgrgbOlyq@ already defined in A.obj

If I switch both to "Use precompiled header", I get in both Debug and Release

如果我将两者都切换到“使用预编译头”,我会同时进入调试和发布

fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object file:

Does anyone know what's going on?

有谁知道发生了什么?

回答by 1800 INFORMATION

You put "create precompiled header" only for stdafx.cpp. Then "use precompiled header" for all of the other ".cpp" files. Finally, have include "stdafx.h"at the start of each ".cpp" file (not usually in the header files.

您只为 stdafx.cpp 放置了“创建预编译头”。然后对所有其他“.cpp”文件“使用预编译头”。最后,include "stdafx.h"在每个“.cpp”文件的开头(通常不在头文件中。

回答by Aidan Ryan

The /Yccompiler option is used to create a pre-compiled header for a compilation action. The /Yuoption instructs the compiler to use a pre-compiled header.

/Yc编译器选项用于创建一个编译动作的预编译的头。该/Yu选项指示编译器使用预编译的头文件。

You will always use the /Yuoption in project settings. In the property pages for your stdafx.cppfile, the /Ycoption will be set.

您将始终使用/Yu项目设置中的选项。在stdafx.cpp文件的属性页中,/Yc将设置该选项。

It is important to understand that there are separate compilation options for each .cppfile .

每个.cpp文件都有单独的编译选项,理解这一点很重要。

See herefor details of the /Y options.

有关/Y 选项的详细信息,请参见此处

回答by GBegen

You put the #pragma oncebefore the #include "stdafx.h"which I think is causing the compiler to ignore the #pragma oncedirective.

你把#pragma once在之前#include "stdafx.h"我认为这是导致编译器忽略的#pragma once指令。

Also, I don't think you should be putting the #include "stdafx.h"line into the header files at all.

另外,我认为您根本不应该将该#include "stdafx.h"行放入头文件中。

回答by MSalters

The results of using "stdafx.h" are not influenced by the PreCompiled Header system. If you turn off Create PCH/Use PCH, the code compiles and creates the same output, except it does so slower. This is also why you can use it in portable code (unlike #pragma once)

使用“stdafx.h”的结果不受预编译头文件系统的影响。如果您关闭 Create PCH/Use PCH,代码将编译并创建相同的输出,但运行速度较慢。这也是您可以在可移植代码中使用它的原因(不像#pragma once