2010-10-11 77 views
8

使用/clr编译选项时,我在VS2008中连接C++项目时出现问题。我正在以下编译错误:使用/ clr时出现LNK2022错误

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指的是。我在网上查了一下,看到微软对这个错误的描述,但是对于它的含义却不知所措。 http://support.microsoft.com/kb/324088

要解决此问题,当你使用控制对C++的扩展,使您避免使用匿名结构作为全局变量添加唯一标识符。

Microsoft已经确认这是本文开头列出的Microsoft产品中的错误。此错误已在Microsoft Visual C++ .NET(2003)中得到纠正。

任何想法将不胜感激。

编辑

上Class1.obj做了反汇编后,我提取两个消息:

// 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) 

我不知道这一切都说明,但它看起来好像第二项是与第一个相同,但属性被定义为向后的例外。

+0

我修改了我的解决方案设置,将单个项目中的所有托管代码包含在内,而不是多个项目,并且错误消失。这将永远是一个谜... – lumberjack4 2010-10-12 16:10:53

回答

4

我今天在我的一个项目上遇到了同样的问题。 我通过重新整理我的头文件来解决它。问题是我把头文件移到了cpp文件的顶部,在包含windows.h的文件之前。 因此,一旦我恢复了包含顺序,并将windows.h放回到cpp文件的顶部,它就修复了一切。

非常奇怪的修复,但它为我工作。

+0

这里是MSDN的链接,它解释了发生了什么。 http://msdn.microsoft.com/en-us/library/aa712965(VS.71).aspx – 2010-12-17 04:14:11

1

这听起来像你正在做这样的事情:

struct 
{ 
    int a; 
    int b; 
} global_struct; 

这是匿名的。如果将其更改为:

struct UniqueNameHere 
{ 
    int a; 
    int b; 
} global_struct; 

应该修复错误。

+1

我没有看到这种情况下弹出我的代码中的任何地方。 – lumberjack4 2010-10-11 20:37:00

+0

PROPSHEETPAGEA和PROPSHEETPAGEW是Windows结构。它在我看来就像这些结构被定义的头(prsht.h)在基于CLR的项目中表现不佳。有没有机会将这个头文件包含在它不需要的地方?您可能需要重新组织代码,以便在解决方案中排除项目不需要的标头。 – 2010-10-11 21:35:04

1

今天我有同样的问题,我的项目从vs2008迁移到vs2010。在编译,与其他C++库链接CLI项目,我得到了这个链接错误:

MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<char,std::char_traits<char>,std::allocator<char> >): (0x0200004f). 
MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >): (0x02000075). 

我试图反汇编如文档建议,但无法找到,这似乎同任何类型定义,但有一个不同的名称。

此外,我必须说我不完全明白这个错误。出现两个错误的事实意味着两种类型(char字符串和wchar字符串)都有两个不同的问题,或者这意味着这两种类型导致的一个问题。这个数字结尾的含义是什么(尽管如此,我在ildasm日志中发现了两个typedef)。文档中没有关于此的信息。

顺便说一句 - 同样的项目 - 相同的配置编译和链接与VS2008完美。

无论如何,在黑暗中迷失,我把我的其他C++库,它在与CLI项目链接,进入CLI项目以及(使用/ clr标志编译它)。重新链接主CLI项目似乎解决了这个问题。以某种方式添加clr支持或与clr链接解决这种类型的冲突。

如果有人对这个问题有一个受过教育的解释,我很乐意阅读它。

1

我正面临同样的问题,但后来我发现在错误中显示的类包含两次。我将我的主类头文件存储到我的项目目录中用于备份目的,原始文件位于我的项目的包含文件夹中。发生这种情况是因为我在项目的“Include Addition File Path”属性中包含了这两个目录路径。

0
MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<char,std::char_traits<char>,std::allocator<char> >): (0x0200004f). 
MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >): (0x02000075). 

此错误只出现在调试版本中,它是某种编译器错误。要解决它,你可以从

MultiThreaded Debug Dll (MDD) 

切换项目设置

Configuration Properties\C/C++\Code Generation\Runtime Library 

MultiThreaded Dll (MD)(为DLL)。但是这样你在调试这段代码的时候会遇到问题(比如带有断点)

另一种方法是摆脱在混合代码中使用stl类:完全在项目中,或者部分:将项目划分为纯原生Stl类)和混合部分,为它们创建不同的预编译头文件,关闭整个项目的/ clr,并且仅为具有混合代码的cpp打开它。

3

我简单地通过对整体solutuion做一个“干净的”来解决这个问题。似乎这只会在VS混合时才会发生。

0

在我的情况下,一个类声明依赖于在.h中声明的某些#define(我测试了#ifdef),我忘记了在其中的一个文件中使用它。结果链接器中有两个不同的类描述。 在这种情况下,使用条件声明检查包含相关文件的所有文件。

0

Stdafx.h中不包含以下声明依赖项目之一:

#ifndef WINVER   // Allow use of features specific to Windows XP or later. 
#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows. 
#endif 

结果是在不同的项目不同的编译文件prsht.h。

在这个文件名是_PROPSHEETPAGE声明的结构。

VS 2008

-1

我最近也有这个问题。看起来,在我制作工作表之前,我已经制作了另一份表格,但删除了它。但不知怎的,它的一个.cpp文件仍然存在。直到我注意到它,我有这个错误。一旦我删除了删除表单的.cpp文件,这个文件就消失了。