I have a VS2010 solution that contains (among others), the following projects:
- Native.DLL (native C++ project that statically links to a third party lib, ITK, which includes STL)
Pseudo-code (very simplified):
using namespace std;
bool Native::CalcSomething(double* result, string& errorMsg);
- Wrapper.DLL (C++/CLI project that dynamically links to Native.DLL and uses std:string in a call to the Native.DLL)
Pseudo-code (very simplified)
bool Wrapper::WrappedCalcSomething([System::Runtime::InteropServices::OutAttribute] double[] result,[System::Runtime::InteropServices::OutAttribute] System::String^ errorMsg)
{
Native* ntv = new Native();
std:string error;
pin_ptr<double> resultPtr = &result[0];
bool success = ntv->CalcSomething(resultPtr, error);
errorMsg = gcnew System::String(error.c_str());
return success;
}
This compiled and linked perfectly in VS2008 (x64), but after migrating to VS2010 (for various reasons), the linker gives the following errors:
2> Generating Code...
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.logic_error): (0x02000049).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.domain_error): (0x0200004a).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.invalid_argument): (0x0200004b).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.length_error): (0x0200004c).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.out_of_range): (0x0200004d).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.runtime_error): (0x0200004e).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.overflow_error): (0x02000050).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.underflow_error): (0x02000051).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std.range_error): (0x02000052).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._Locinfo): (0x02000054).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (_Locimp): (0x02000059).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (failure): (0x02000068).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_val<char,std::allocator<char> >): (0x02000097).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_val<wchar_t,std::allocator<wchar_t> >): (0x02000099).
2>MSVCMRTD.lib(locale0_implib.obj) : error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (lconv): (0x020000ce).
2>LINK : fatal error LNK1255: link failed because of metadata errors
I have read almost every thread I could find on this issue and have tried:
- Clean and rebuild
- Move the headers around - there is no reference to windows.h and I have tried moving the #include line around to no effect
- Using the /clr flag only on the files that need it in Wrapper.DLL (this happens to be all the files in the project). The problem is that the public method in Native.DLL that needs to be invoked by Wrapper.DLL includes a std:string parameter in its signature. This makes it difficult to separate the STL reference out from the scope of the /clr compilation
- I am compiling everything (including ITK, the 3rd party lib) with /MDd (and have tried /MD) - changing this setting does not seem to affect things
- I do not see /Zp or pragma pack being used anywhere
The only "solution" I can think of is to change the method in Native.DLL to not use std:string as a parameter (e.g. use char* instead). However, to avoid the use of STL in any C++/CLI wrappers hardly seems like a solution. There must be a better way!
Note: I am aware of the article on how to "debug" these issues (link), but unless I'm mistaken, I think I know the source of the issue is the std:string.