我有很多项目的大解决方案,使用 VS2008 SP1,并且每天至少遇到一次 LNK2022 错误。如果我完全重建解决方案,它构建得很好,但这并不有趣。
它发生在依赖 DLL 被“不显着”地改变(即没有改变任何方法或类),并且后来构建了引用项目时。合并元数据时失败 - 不管这意味着什么。
首先要注意的是,共享 DLL 是#using
从多个 .CPP 文件中引用的。
第二件事是,如果我从共享 DLL 中删除 AssemblyInfo.cpp,那么问题就会消失(但我不确定这是否是一个明智的修复?)。
我已尽可能将其缩小为包含 2 个 CLR 类库项目的以下解决方案( xxx项目依赖于Shared):
以下是每个文件的内容:
共享.cpp:
public ref class Shared
{
};
英寸:
#pragma once
#using "Shared.dll"
public ref class Common
{
private:
Shared^ m_fred;
};
xxx.cpp 和 xxx2.cpp:
#include "inc.h"
要重现,首先重建解决方案。它会建立好的。
现在保存Shared.cpp并构建解决方案,它将构建良好并显示:
...
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并构建解决方案,它失败并显示以下消息:
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 ==========
编辑:
xxx.obj 和 xxx2.obj 的 IL 之间的区别如下:
(for xxx.obj)
// AssemblyRef #2 (23000002)
// ---------------------------------- ---------------------
// 令牌:0x23000002
// 公钥或令牌:
// 名称:共享
// 版本:1.0.3412.16 606
// 主要版本: 0x00000001
// 次要版本: 0x00000000
// 内部版本号: 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
//标志:[无](00000000)
(for xxx2.obj)
// AssemblyRef #2 (23000002)
// ---------------------------------- ---------------------
// 令牌:0x23000002
// 公钥或令牌:
// 名称:共享
// 版本:1.0.3412.16 585
// 主要版本: 0x00000001
// 次要版本: 0x00000000
// 内部版本号: 0x00000d54
// 修订号: 0x000040 c9
// 区域设置:
// HashValue Blob: 64 af d3 12 9d e3 f6 2b 59 ac ff e5 3b 38 f8 fc 6d f4 d8 b5
//标志:[无](00000000)
这对我来说意味着 xxx2.obj 仍在使用旧版本的 Shared.dll,这与使用更新的 Shared.dll 的 xxx.obj 冲突。那么我该如何解决呢?