问题描述
我有一个大的解决方案与许多项目,使用VS2008 SP1,并至少每天我遇到LNK2022错误一次。如果我做一个完全重建的解决方案,它构建良好,但这是没有乐趣。
I have a big solution with lots of projects, using VS2008 SP1, and at least once a day I encounter the LNK2022 error. If I do a full rebuild of the solution it builds fine, but this is not fun.
这发生在一个依赖DLL被无意义地改变(即没有改变任何方法或类),并且稍后构建引用项目。
It happens when a dependent DLL is changed 'insignificantly' (i.e. without changing any methods or classes), and the referencing project is later built. It fails when merging the metadata - whatever that means.
首先要注意的是,共享的DLL被引用 #using
从多个.CPP文件。
第二件事是,如果我从共享DLL中删除AssemblyInfo.cpp,则问题消失了(但我)。
First thing to note is that the shared DLL is referenced with #using
from multiple .CPP files.
Second thing is that if I delete the AssemblyInfo.cpp from the shared DLL then the problem goes away (but I'm not sure if this is a sensible fix?).
尽可能将其缩小到包含2个CLR类库项目的下列 xxx 项目取决于共享):
I've narrowed it down as far as possible into the following solution containing 2 CLR Class Library projects (the xxx project depends on Shared):
这里是每个文件的内容:
Here are the contents of each file:
public ref class Shared
{
};
inc.h:
inc.h:
#pragma once
#using "Shared.dll"
public ref class Common
{
private:
Shared^ m_fred;
};
xxx.cpp和xxx2.cpp:
xxx.cpp and xxx2.cpp:
#include "inc.h"
首先重建解决方案。
现在保存 Shared.cpp 并构建解决方案,它会正常显示:
To reproduce, first rebuild the solution. It will build OK.
Now save Shared.cpp and build the solution, it will build fine and show:
...
2>------ Build started: Project: xxx, Configuration: Debug Win32 ------
2>Inspecting 'd:\xxx\xxx\Debug\Shared.dll' changes ...
2>No significant changes found in 'd:\xxx\xxx\Debug\Shared.dll'.
2>xxx - 0 error(s), 0 warning(s)
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
现在储存 xxx.cpp 并构建解决方案,它失败,并显示以下消息:
Now save xxx.cpp and build the solution, it fails with the following message:
1>------ Build started: Project: xxx, Configuration: Debug Win32 ------
1>Compiling...
1>xxx.cpp
1>Linking...
1>xxx2.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Common; fields: m_fred): (0x04000001).
1>LINK : fatal error LNK1255: link failed because of metadata errors
1>xxx - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
EDIT :
IL for xxx.obj和xxx2.obj之间的区别如下:
EDIT:
The differences between the IL for xxx.obj and xxx2.obj are as follows:
(for xxx.obj)
// AssemblyRef#2(23000002)
// ------------------------------------------------ -------
//令牌:0x23000002
//公共密钥或令牌:
//名称:Shared
//版本:1.0.3412.16 606
//主要版本:0x00000001
//次要版本:0x00000000
// Build Number:0x00000d54
//修订版本号:0x000040 de
//语言环境: >
// HashValue Blob: 1c bb 8f 13 7e ba 0a c7 26 c6 fc cb f9 ed 71 bf 5d ab b0 c0
//标志:[ none](00000000)
(for xxx.obj)
// AssemblyRef #2 (23000002)
// -------------------------------------------------------
// Token: 0x23000002
// Public Key or Token:
// Name: Shared
// Version: 1.0.3412.16606
// Major Version: 0x00000001
// Minor Version: 0x00000000
// Build Number: 0x00000d54
// Revision Number: 0x000040de
// Locale:
// HashValue Blob: 1c bb 8f 13 7e ba 0a c7 26 c6 fc cb f9 ed 71 bf 5d ab b0 c0
// Flags: [none] (00000000)
(适用于xxx2.obj)
// AssemblyRef#2(23000002)
// ---------------------------------- ---------------------
//令牌:0x23000002
//公共密钥或令牌:
//名称:共享
//版本:1.0.3412.16 585
//主要版本:0x00000001
//版本号:0x00000000
//版本号:0x00000d54
//修订版号:0x000040 c9 br />
//语言环境:
// HashValue Blob: 64 af d3 12 9d e3 f6 2b 59 ac ff e5 3b 38 f8 fc 6d f4 d8 b5
//标记:[无](00000000)
(for xxx2.obj)
// AssemblyRef #2 (23000002)
// -------------------------------------------------------
// Token: 0x23000002
// Public Key or Token:
// Name: Shared
// Version: 1.0.3412.16585
// Major Version: 0x00000001
// Minor Version: 0x00000000
// Build Number: 0x00000d54
// Revision Number: 0x000040c9
// Locale:
// HashValue Blob: 64 af d3 12 9d e3 f6 2b 59 ac ff e5 3b 38 f8 fc 6d f4 d8 b5
// Flags: [none] (00000000)
这意味着xxx2.obj仍在使用旧版本的Shared .dll,这与使用更新的Shared.dll的xxx.obj冲突。所以我怎么可以
This implies to me that xxx2.obj is still using the old version of Shared.dll, and that is conflicting with xxx.obj which is using the updated Shared.dll. So how can I wo
推荐答案
Microsoft回复了我的Connect帖子,有一个更好的解决方法:
Microsoft replied to my Connect post, with a much better workaround:
[assembly:AssemblyVersionAttribute(1.0。*)];
[assembly:AssemblyVersionAttribute("1.0.*")];
与
[assembly:AssemblyVersionAttribute(1.0.0.1)];
[assembly:AssemblyVersionAttribute("1.0.0.1")];
cpp。这将确保
版本不会在增量构建之间更改
。
in AssemblyInfo.cpp. This will ensure that the version does not change between incremental builds.
这是最好禁用该功能。
无论如何,接受的答案已被选择,现在无法更改:(
This works for me and obviously this is preferable to disabling the feature.
Anyway the accepted answer has been chosen and cannot be changed now :(
这篇关于“LNK2022:元数据操作失败”驱使我疯了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!