LNK2022 Error When Using /clr

馋奶兔 提交于 2019-11-29 13:35:49

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.

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:

struct
{
    int a;
    int b;
} global_struct;

which is anonymous. If you change it to:

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

that should fix up the errors.

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:

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

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.

Chirag

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.

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

This error appears only in debug version, it's some kind of compiler bug. To fix it you can switch project setting

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

from

MultiThreaded Debug Dll (MDD)

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.

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.

Mikhael

In one of the dependent projects stdafx.h did not contain the following declaration:

#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

The result is different compilation file prsht.h in various projects.

In this file name is declared structure with _PROPSHEETPAGE.

VS 2008

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!