c++ - LNK2022 使用/clr 时出错

标签 c++ visual-studio-2008 linker clr managed-c++

使用 /clr 编译选项时,我在 VS2008 中链接 C++ 项目时遇到问题。我收到以下构建错误:

Class1.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046f).
Class1.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000473).
Class2.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046f).
Class2.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000473).
Class3.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046e).
Class3.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000472).
Class4.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046e).
Class4.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000472).
Class5.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046e).
Class5.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000472).
Class6.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEA): (0x0200046e).
Class6.obj : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_PROPSHEETPAGEW): (0x02000472).
LINK : fatal error LNK1255: link failed because of metadata errors

我不知道 PROPSHEETPAGEAPROPSHEETPAGEW 指的是什么。我在网上查看了 Microsoft 对错误的描述,但对它的含义一头雾水。 http://support.microsoft.com/kb/324088

To resolve this problem, add unique identifiers when you use Managed Extensions for C++ so that you avoid using anonymous structures as global variables.

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Visual C++ .NET (2003).

任何想法将不胜感激。

编辑

在对 Class1.obj 执行 ildasm 之后,我提取了两条消息:

// TypeDef #1134 (0200046f)
// -------------------------------------------------------
//  TypDefName: _PROPSHEETPAGEA  (0200046F)
//  Flags     : [NotPublic] [SequentialLayout] [Class] [Sealed] [AnsiClass] [BeforeFieldInit]  (00100108)
//  Extends   : 0100000B [TypeRef] System.ValueType
//  Layout    : Packing:0, Size:56
//  CustomAttribute #1 (0c0012a0)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000003
//      CustomAttributeName: Microsoft.VisualC.MiscellaneousBitsAttribute :: instance void .ctor(int32)
//      Length: 8
//      Value : 01 00 41 00 00 00 00 00                          >  A             <
//      ctor args: (65)
// 
//  CustomAttribute #2 (0c0012a1)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000001
//      CustomAttributeName: Microsoft.VisualC.DebugInfoInPDBAttribute :: instance void .ctor()
//      Length: 4
//      Value : 01 00 00 00                                      >                <
//      ctor args: ()
// 
//  CustomAttribute #3 (0c0012a2)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000004
//      CustomAttributeName: System.Runtime.CompilerServices.NativeCppClassAttribute :: instance void .ctor()
//      Length: 4
//      Value : 01 00 00 00                                      >                <
//      ctor args: ()


// TypeDef #1138 (02000473)
// -------------------------------------------------------
//  TypDefName: _PROPSHEETPAGEW  (02000473)
//  Flags     : [NotPublic] [SequentialLayout] [Class] [Sealed] [AnsiClass] [BeforeFieldInit]  (00100108)
//  Extends   : 0100000B [TypeRef] System.ValueType
//  Layout    : Packing:0, Size:56
//  CustomAttribute #1 (0c0012b0)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000004
//      CustomAttributeName: System.Runtime.CompilerServices.NativeCppClassAttribute :: instance void .ctor()
//      Length: 4
//      Value : 01 00 00 00                                      >                <
//      ctor args: ()
// 
//  CustomAttribute #2 (0c0012b1)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000001
//      CustomAttributeName: Microsoft.VisualC.DebugInfoInPDBAttribute :: instance void .ctor()
//      Length: 4
//      Value : 01 00 00 00                                      >                <
//      ctor args: ()
// 
//  CustomAttribute #3 (0c0012b2)
//  -------------------------------------------------------
//      CustomAttribute Type: 0a000003
//      CustomAttributeName: Microsoft.VisualC.MiscellaneousBitsAttribute :: instance void .ctor(int32)
//      Length: 8
//      Value : 01 00 41 00 00 00 00 00                          >  A             <
//      ctor args: (65)

我不确定这一切意味着什么,但看起来第二个条目与第一个条目相同,除了向后定义的属性。

最佳答案

我今天在我的一个项目中遇到了完全相同的问题。 我通过重新排列我的头文件来解决它。问题是我将头文件移动到 cpp 文件的顶部,在包含 windows.h 的文件之前。 所以,一旦我恢复了包含顺序,并将 windows.h 放回 cpp 文件的顶部,它就修复了所有问题。

非常奇怪的修复,但对我有用。

关于c++ - LNK2022 使用/clr 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3909470/

相关文章:

visual-studio - 如何在 Visual Studio 2008 中使用 Visual Studio 2010 C++ 工具?

c - 为什么将额外参数传递给 C 函数不会导致编译时错误?

c++ - 使用带有 mingw-w64 的 Allegro 5 的未定义引用

c++ - 如何使用 GCC 创建基于其他静态库的静态库?

c++ - 如何测量一个线程的执行时间?

c++ - ISO C++ 禁止声明没有类型的 'Stack'

c++ - for的递归

python - 使用 ctypes 和 wchar_t 时如何将字符串从 C++ 获取到 python?

c++ - 使用 Visual Studio 2008 构建 OpenCV 应用程序并从另一台计算机运行它

visual-studio-2010 - Visual Studio 以管理员身份运行快捷方式