简体   繁体   中英

Statically linked unmanaged libs and C++ CLR

Is it possible to use to use libs compiled with /MT in C++ CLR? It throws me either a ton of LNK2022 "metadata operation failed (8013118D)" errors (if I use /MD in the CLR project) or " '/MT' and '/clr:pure' command-line options are incompatible" if I use /MT.

What do I need to change in the library? The library is mine, but it includes several third party static libs.

LNK2022 are a pain to pinpoint. It usually means one of your module's configuration affecting structure layout is different from the others.

Check for the following usual causes:

  • Make sure all your projects are using the same runtime library (/MDd or /MD) for your current solution configuration. If one project is using Debug while others are using Release or vice-versa, you will get LNK2022 errors.
  • Make sure all your projects are using the same structure member alignment. Pay special attention if one project is using /Zp switch. Also, make sure you dont use #pragma pack(n) conditionally.

You can use /d1reportSingleClassLayout_your-class-name_ (without space) to get information about the problematic class' layout.

For more information see : Diagnosing Hidden ODR Violations in Visual C++

The only way I found to mix native code static libraries compiled with different crt runtime versions, is to write a DLL that acts like a bridge between the libraries. For example:

your.exe - compiled with /MD and clr yourbridge.dll - native, compiled with /MT and includes all the 3rd party libs built using /MT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM