Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK2022 (Duplicate managed types have different visibilities) on MSVS 2012

I'm porting a solution from MSVS2005 to MSVS2012. The projects are in C++ .NET but use homemade native C++ libraires too. We had no problem building the projects with 2005 but now, I'm unable to build a project using 2012. I get the following error message:

MyFile.obj : error LNK2022: metadata operation failed (801311E4) : Duplicate managed types have different visibilities.

What does this mean? What info do you need to help me?

Thanks for your help?

like image 861
dom_beau Avatar asked Sep 28 '12 18:09

dom_beau


1 Answers

I had the same problem, and indeed had the same condition described in dom_beau's answer, so I'm pretty sure I had the same underlying cause as well. However, to be able to resolve the error, I had to find the actual offending classes (there were a few, and the error messages do little to help you find them!).

So I wrote the following LINQ query which finds all classes defined in multiple *.obj files with conflicting visibilities. It may be useful to someone, so I'm posting it here.

// Analyze text files produced by ildasm when given *.obj files.
// Use "for %1 in (*.obj) do ildasm /text %1 > %1-ildasm.txt" to produce the files.

from file in Directory.GetFiles(@"your project's intermediate folder")
where file.EndsWith("-ildasm.txt")
let lines = File.ReadAllLines(file)
from i in Enumerable.Range(0, lines.Count() - 1)
where lines[i].Contains("TypDefName:")
let type = lines[i].Substring(16,lines[i].IndexOf(" (")-17)
let flags = lines[i+1]
group new {file, flags} by type into g
where g.Select(t=>t.flags).Distinct().Count() > 1
select g
like image 51
Yodan Tauber Avatar answered Nov 14 '22 16:11

Yodan Tauber