Saturday, 23 March 2013

LNK1255: link failed because of metadata errors


I got this error while playing around with C++ VS 2010 projects.

On my configuration I had several MFC DLL projects. One of them with a configuration type of static library (.lib) and the others were Application (.exe). All of them use MFC in a Shared DLL and have Multi-threaded Debug DLL (/MDd) on the Run Time option of the Code Generation setting.

It occurred to me to add a new project that will act as a Unit Test for the Application (.exe) and the Library (.lib) projects.

I read about NUnit and decided to give it a go. Now all of the projects are unmanaged C++  whereas the project for the Unit Test had to support managed code  as I decided to use Nunit.

I got some cool instructions on how to set up a managed unit test project with native C++ code here:

http://www.codinginlondon.com/2008/11/using-nunit-with-native-c.html

After creating the managed C++(CLR) class project and following the instructions on that blog I was ready to build the solution and started mocking around with the Unit Test.

Well, while building the solution I got these error messages:


Error 23 error LNK1255: link failed because of metadata errors
Error 17 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_const_iterator,std::allocator >): (0x02000019).
Error 20 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_iterator,std::allocator >): (0x02000080).
Error 21 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_val >): (0x02000081)

Ummm. no good. Not good at all. I did some research using Google and found few hints but none of them gave me the right solution or just didn't apply to my configuration.

http://stackoverflow.com/questions/4842205/getting-error-when-compiling-debug-mode-c-cli-error-lnk2022
http://stackoverflow.com/questions/11990095/lnk2022-metadata-operation-inconsistent-layout-information-in-duplicated-types

The last stackoverlfow question and answer looked very promising. however , it didn't work for me. So I re-built the solution and copied the warning and error messages, as displayed on the output window of Visual Studio 2012, and went through it one by one.

I found a warning that was interesting:

defaultlib 'msvcrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library.

This warning came from the managed C++ Class Project. The one containing the Unit Tests. ummm.

The message tell me that there is a conflict of library between the projects. First thing it came to my mind was to go back and check the Code Generation settings, particularly the  Run Time option.

It was then that I noticed that the Managed class project acting as the Unit Tests Project had a Multi-threaded DLL (/MD) setting on the Run Time option of the Code Generation settings.

On the other hand all of the other projects, native/un-managed C++, had the Run Time as Multi-threaded Debug DLL (/MDd)

So I changed the Run Time setting for the managed class project  to  Multi-threaded Debug DLL (/MDd) and all the link errors disappeared.

For a good explanation of the CRT link libraries (Run Time option) You can visit this link to a QA on
stackoverflow: http://stackoverflow.com/questions/3007312/resolving-lnk4098-defaultlib-msvcrt-conflicts-with

Until next time, bye.