如何在 MSVC 中使用使用 MingW 编译的库?

我用 MingW/MSYS 编译了几个库……生成的静态库总是 .a 文件.当我尝试将库与 MSVC 项目链接时,Visual Studio 抛出未解析的外部符号"......这意味着 .a 静态库与 MS C++ 链接器不兼容.我认为它必须转换为兼容 MSVC 的 .lib 文件.

I have compiled several libraries with MingW/MSYS... the generated static libraries are always .a files. When I try to link the library with a MSVC project, Visual Studio throws 'unresolved external symbols' ... It means that the .a static library is incompatible with MS C++ Linker. I presume it has to be converted to a MSVC compatible .lib file.

.a 和 .lib 只是 .o 或 .obj 文件的 AR 档案,那么有什么方法可以在 MSVC 项目中使用 MingW 编译的库?或者我是否必须仅在一个编译器/链接器中编译/链接所有内容 - 仅限 MSVC/仅限 MingW?据说 MingW 编译器与 MSVC 兼容.

Either .a and .lib are just AR archives of .o or .obj files, so is there any way how to use MingW compiled libs in a MSVC project? Or do I have to compile/link everything just in one compiler/linker - MSVC only/MingW only? The MingW compiler is said to be compatible with MSVC.

我阅读了一些关于这个主题的帖子,但他们大多说将文件重命名为 .lib 应该可以解决问题,但不幸的是它对我不起作用.

I read a few threads about this topic, but they mostly say that renaming the file to .lib should do the work, but it unfortunately doesn't work for me.

我尝试链接的库是用 C 编写的.

The libraries Im trying to link are written in C.

MSVC 链接器抛出如下错误:

MSVC Linker throws errors like:

error LNK2019: unresolved external symbol "int __cdecl openssl_call(struct ssl_State *,int,int,int)" (?openssl_call@@YAHPAUssl_State@@HHH@Z) referenced in function _main MyAPP.obj

...还有 4 个相同的错误,这些错误涉及从我的应用中调用的其他函数.

... and 4 more same errors referring to other functions called from my app.

感谢您的建议.

推荐答案

基于此错误,您发表评论:

Based on this error you put in a comment:

错误 LNK2019:未解析的外部符号int __cdeclopenssl_call(struct ssl_State*,int,int,int)" (?openssl_call@@YAHPAUssl_State@@HHH@Z)在函数 _main MyAPP.obj 中引用所有其他 4 个错误仅与其他函数名称

error LNK2019: unresolved external symbol "int __cdecl openssl_call(struct ssl_State *,int,int,int)" (?openssl_call@@YAHPAUssl_State@@HHH@Z) referenced in function _main MyAPP.obj all other 4 errors are same only with other functions names

尝试将 extern "C" 放在 openssl 的包含文件周围.例如:

Try putting extern "C" around your include files for openssl. For example:

extern "C" {
include "openssl.h"
}

使用 extern "C"指示编译器函数使用的是 C 链接,而不是 C++,这将阻止它执行 name mangling 关于函数.所以它将在库中查找函数 openssl_call 而不是 ?openssl_call@@YAHPAUssl_State@@HHH@.

using extern "C" will instruct the compiler that the functions are using C linkage, not C++, which will stop it from performing name mangling on the functions. So it will look for the function openssl_call in the library rather than ?openssl_call@@YAHPAUssl_State@@HHH@.

相关文章