c++ - Visual C++ 预编译头错误

标签 c++ visual-studio visual-c++ header precompiled-headers

更新:

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


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

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

这是我的 stdafx.h

#pragma once

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

还有我的 stdafx.cpp

#include "stdafx.h"

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

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

对于发布和调试,我都有“创建预编译头文件 (/Yc)”。它在 Debug模式下编译良好,但在 Release模式下它会不断报告

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

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

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

有人知道怎么回事吗?

最佳答案

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

关于c++ - Visual C++ 预编译头错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1397190/

相关文章:

c# - SQLite 的 dotConnect 包不适用于 VS2013 中的 64 位 C# 项目?

c++ - 在 Visual C++ 中进行困难测量后对光学 3D 传感器进行编程

c++ - 在 Crypto++ 中加载 PEM 编码的私有(private) RSA key

c++ - lex 和 yacc 中的词法分析器没有输出

c++ - 带有 volatile 的 push_back 与 emplace_back

visual-studio - TFS2012 : Add label at a specific changeset in version control?

c++ - 在 MARSS 实现中编译 SCONS 失败

visual-studio - Sitefinity 5.3 : How can I place a widget into my Visual Studio template?

c++ - OpenGL坐标转换屏幕到世界

c++ - 获取调用者的返回地址