使用 /clr 时出现 LNK2022 错误
使用 /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
我不知道 PROPSHEETPAGEA
和 PROPSHEETPAGEW
指的是什么。我在网上查看了微软对该错误的描述,但不知道它的含义。 http://support.microsoft.com/kb/324088
要解决此问题,请在使用 C++ 托管扩展时添加唯一标识符,以避免使用匿名结构作为全局变量。
Microsoft 已确认这是本文开头列出的 Microsoft 产品中的错误。此错误已在 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)
我不确定这一切意味着什么,但看起来第二个条目与第一个条目相同,但属性除外被向后定义。
I'm having a problem linking a C++ project in VS2008 when using the /clr
compile option. I am getting the following build errors:
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
I have no idea what PROPSHEETPAGEA
and PROPSHEETPAGEW
are referring to. I checked online to see Microsoft's description of the error but am at a loss as to what it means.
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).
Any ideas would be greatly appreciated.
EDIT
After doing a ildasm on Class1.obj I extracted two messages:
// 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)
I'm not sure what all this means, but it looks as if the second entry is identical to the first with the exception of the attributes being defined backwards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我今天在我的一个项目中遇到了完全相同的问题。
我通过重新排列头文件解决了这个问题。问题是我已将头文件移动到 cpp 文件的顶部,位于包含 windows.h 的文件之前。
因此,一旦我恢复了包含顺序,并将 windows.h 放回到 cpp 文件的顶部,它就解决了所有问题。
非常奇怪的修复,但它对我有用。
I had this exact same problem today on one of my projects.
I solved it by rearranging my header files. Problem was I had moved a header file to the top of the cpp file, before the file that was including windows.h.
So, once I reverted the include order, and put windows.h back at the top of the cpp file, it fixed everything.
very wierd fix, but it worked for me.
我只是通过对整体解决方案进行“清理”来解决这个问题。看来如果 VS 混淆了事情就会发生这种情况。
I solved this issue simply by doing a "clean" on the overall solutuion. Seems that this just can happen if VS mixes things up.
听起来你正在做这样的事情:
这是匿名的。如果将其更改为:
应该可以修复错误。
It sounds like you're doing something like this:
which is anonymous. If you change it to:
that should fix up the errors.
我今天遇到了同样的问题,将我的项目从vs2008迁移到vs2010。在编译与其他 C++ 库链接的 CLI 项目时,我收到此链接错误:
我按照文档建议尝试了 ildasm,但找不到任何看起来相同但名称不同的类型定义。
此外,我必须说我并不完全理解这个错误。出现两个错误这一事实意味着这两种类型(char 字符串和 wchar 字符串)都存在两个不同的问题,或者是否意味着这两种类型导致了同一个问题。该行末尾的数字的含义是什么(不过,我确实在 ildasm 日志上找到了两个带有这些数字的 typedef)。文档中没有任何相关内容。
顺便说一句 - 相同的项目 - 相同的配置与 vs2008 完美编译和链接。
不管怎样,在黑暗中迷失了方向,我将另一个与 CLI 项目链接的 C++ 库也变成了一个 CLI 项目(使用 /clr 标志编译它)。重新链接主 CLI 项目似乎可以解决问题。以某种方式添加 clr 支持或与 clr 链接可以解决这种类型的冲突。
如果有人对这个问题有一个有根据的解释,我很乐意阅读。
I had the same problem today, migrating my project from vs2008 to vs2010. While compiling a CLI project that links with other C++ libs, i got this link error on:
I tried ildasm as the documentation suggest, but could not find any type definitions that seems the same but having a different names.
moreover, I must say I do not completely understand this error. The fact that two errors appear means that there are two different problems with both types (char string and wchar string) or does it mean that there is one problem that these two types are causing. What is the meaning of the numbers in the end of the line (i did find two typedefs on the ildasm log with these numbers, though). There is nothing about that in the documentation.
BTW - same projects -same configuration compiles and links perfectly with vs2008.
Anyway, lost in the dark, I turned my other C++ lib, which link with the CLI project, into a CLI project as well (compiling it with /clr flag). relinking the main CLI project seems to solve the problem. Somehow adding the clr support or linking with clr resolve this types collision.
If someone have an educated explanation to this issue I would love to read it.
我遇到了同样的问题,但后来我发现错误中显示的类被包含了两次。我已将主类头文件存储到项目目录中以供备份,而原始头文件位于项目的 include 文件夹中。发生这种情况是因为我已将这两个目录路径包含在项目的“包含附加文件路径”属性中。
I was facing the same kind of problem, but later on I found that the class which was shown in the error was included twice. I had stored my main class header file into my project directory for backup purposes, and the original one was residing in the include folder of my project. It happened because I had included these two directory paths in my project's "Include Addition File Path" property.
此错误仅出现在调试版本中,它是某种编译器错误。要修复此问题,您可以将项目设置
从
“MultiThreaded Dll (MD)”切换为“MultiThreaded Dll (MD)”(对于 DLL)。但是这样你在调试这段代码时就会遇到问题(例如使用断点)
另一种方法是摆脱在混合代码中使用 stl 类:完全在项目中,或部分:将项目划分为纯本机(带有 Stl 类)和混合部分,为它们创建不同的预编译头,关闭整个项目的 /clr,并仅针对具有混合代码的 cpp 打开它。
This error appears only in debug version, it's some kind of compiler bug. To fix it you can switch project setting
from
to
MultiThreaded Dll (MD)
(for DLL). But then you'll have problems with debugging this code (e.g. with breakpoints)Another way is to get rid of using stl classes in mixed code: completely in the project, or partly: divide your project into pure native (with Stl classes) and mixed parts, create different precompiled headers for them, switch off /clr for the whole project, and switch it on only for cpp's with mixed code.
在我的例子中,一个类声明依赖于 .h 中声明的一些
#define
(测试#ifdef
),我忘记将其包含在使用它的文件之一中。结果链接器对该类有两种不同的描述。在这种情况下,检查包含带有条件声明的相关文件的所有文件。
In my case one class declaration was dependent on some
#define
declared in a .h (testing#ifdef
)which I forgot to include in one of the files using it. In result linker had 2 different descriptions of the class.In this situation check all files that include the related file with conditional declaration.
在其中一个依赖项目 stdafx.h 中不包含以下声明:
结果是各个项目中的编译文件 prsht.h 不同。
在此文件名中使用 _PROPSHEETPAGE 声明结构。
对比2008年
In one of the dependent projects stdafx.h did not contain the following declaration:
The result is different compilation file prsht.h in various projects.
In this file name is declared structure with _PROPSHEETPAGE.
VS 2008
我最近也遇到了这个问题。看来在我制作工作表单之前,我已经制作了另一个表单但将其删除了。但不知怎的,它的 .cpp 文件之一仍然存在。直到我注意到它时,我才发现这个错误。一旦我删除了已删除表单的 .cpp 文件,问题就消失了。
I too had this problem recently. It seems that before I made a my working form, I had made another form but deleted it. But somehow one of its .cpp file remained. Till the time I noticed it, I had this error. Once I removed the .cpp file of the deleted form, the prblem went away.