Visual C++预编译头错误
更新:
在我的头文件中包含 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)”。它在调试模式下编译得很好,但在发布模式下它不断报告
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:
有人知道发生了什么吗?
Update:
What are the effects of including stdafx.h in my header files?
I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows.
In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h.
Here's my stdafx.h
#pragma once
#include <string>
#include <vector>
#include <map>
...
and my stdafx.cpp
#include "stdafx.h"
In every .h and .cpp file, I have the following:
#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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您仅为 stdafx.cpp 添加“创建预编译头”。然后对所有其他“.cpp”文件“使用预编译头”。最后,在每个“.cpp”文件的开头包含“stdafx.h”(通常不在头文件中)。
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./Yc
编译器选项用于为编译操作创建预编译头。/Yu
选项指示编译器使用预编译头。您将始终在项目设置中使用
/Yu
选项。在
stdafx.cpp
文件的属性页中,将设置/Yc
选项。重要的是要了解每个
.cpp
文件都有单独的编译选项。
有关 /Y 选项的详细信息,请参阅此处。
The
/Yc
compiler option is used to create a pre-compiled header for a compilation action. The/Yu
option instructs the compiler to use a pre-compiled header.You will always use the
/Yu
option in project settings.In the property pages for your
stdafx.cpp
file, the/Yc
option will be set.It is important to understand that there are separate compilation options for each
.cpp
file.
See here for details of the /Y options.
您将
#pragma Once
放在#include "stdafx.h"
之前,我认为这会导致编译器忽略#pragma Once
指令。另外,我认为您根本不应该将
#include "stdafx.h"
行放入头文件中。You put the
#pragma once
before the#include "stdafx.h"
which I think is causing the compiler to ignore the#pragma once
directive.Also, I don't think you should be putting the
#include "stdafx.h"
line into the header files at all.使用“stdafx.h”的结果不受预编译头系统的影响。如果关闭“创建 PCH”/“使用 PCH”,代码将编译并创建相同的输出,只是速度较慢。这也是您可以在可移植代码中使用它的原因(与
#pragma Once
不同)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
)