静态链接的非托管库和C ++ CLR

时间:2022-06-02 13:19:35

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.

是否可以使用在C ++ CLR中使用/ MT编译的库?它抛出了大量的LNK2022“元数据操作失败(8013118D)”错误(如果我在CLR项目中使用/ MD)或“'/ MT'和'/ clr:pure'命令行选项是不兼容的”如果我使用/ MT。

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

我需要在库中更改什么?该库是我的,但它包含几个第三方静态库。

2 个解决方案

#1


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

LNK2022很难确定。它通常意味着影响结构布局的模块配置之一与其他模块不同。

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.
  • 确保所有项目都使用相同的运行时库(/ MDd或/ MD)作为当前解决方案配置。如果一个项目正在使用Debug而其他项目正在使用Release,反之亦然,则会出现LNK2022错误。

  • 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.
  • 确保所有项目都使用相同的结构成员对齐。如果一个项目正在使用/ Zp开关,请特别注意。另外,请确保您不要有条件地使用#pragma pack(n)。

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

您可以使用/ d1reportSingleClassLayout_your-class-name_(不带空格)来获取有关有问题的类布局的信息。

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

有关详细信息,请参阅:在Visual C ++中诊断隐藏的ODR违规

#2


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:

我发现混合使用不同crt运行时版本编译的本机代码静态库的唯一方法是编写一个类似于库之间桥梁的DLL。例如:

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

your.exe - 使用/ MD和clr yourbridge.dll编译 - 本机,使用/ MT编译,包括使用/ MT构建的所有第三方库。

#1


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

LNK2022很难确定。它通常意味着影响结构布局的模块配置之一与其他模块不同。

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.
  • 确保所有项目都使用相同的运行时库(/ MDd或/ MD)作为当前解决方案配置。如果一个项目正在使用Debug而其他项目正在使用Release,反之亦然,则会出现LNK2022错误。

  • 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.
  • 确保所有项目都使用相同的结构成员对齐。如果一个项目正在使用/ Zp开关,请特别注意。另外,请确保您不要有条件地使用#pragma pack(n)。

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

您可以使用/ d1reportSingleClassLayout_your-class-name_(不带空格)来获取有关有问题的类布局的信息。

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

有关详细信息,请参阅:在Visual C ++中诊断隐藏的ODR违规

#2


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:

我发现混合使用不同crt运行时版本编译的本机代码静态库的唯一方法是编写一个类似于库之间桥梁的DLL。例如:

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

your.exe - 使用/ MD和clr yourbridge.dll编译 - 本机,使用/ MT编译,包括使用/ MT构建的所有第三方库。