Visual C++预编译标头错误

jam*_*o00 15 c++ header precompiled-headers visual-studio 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>
...
Run Code Online (Sandbox Code Playgroud)

和我的stdafx.cpp

#include "stdafx.h"
Run Code Online (Sandbox Code Playgroud)

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

#pragma once //if in a header file
#include "stdafx.h"
Run Code Online (Sandbox Code Playgroud)

对于发布和调试,我都有"创建预编译头(/ Yc)".它在调试模式下编译良好,但在发布模式下它保持报告

error LNK2005: ___@@_PchSym_@00@UfhvihUaszlaDUwlxfnvmghUnnlUhixUnnlPeDUnnlPeDUivovzhvUvmgrgbOlyq@ already defined in A.obj
Run Code Online (Sandbox Code Playgroud)

如果我同时切换到"使用预编译头",我会进入Debug和Release

fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object file:
Run Code Online (Sandbox Code Playgroud)

有谁知道发生了什么?

180*_*ION 30

您只为stdafx.cpp添加"create precompiled header".然后为所有其他".cpp"文件"使用预编译头".最后,include "stdafx.h"在每个".cpp"文件的开头(通常不在头文件中).

  • 在VS中,您可以右键单击单个文件并选择"属性".然后在"C++ - >预编译头"下,您可以将值设置为"create"或"use".配置范围设置用作默认设置 - 您应该将其设置为"use",然后将其覆盖为stdafx.cpp (10认同)

Aid*_*yan 6

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

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

重要的是要了解每个.cpp文件都有单独的编译选项.

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