从 MinGW 链接到 MSVC DLL

2021-12-18 00:00:00 gcc visual-c++ g++ mingw c++

我正在尝试将 LizardTech GeoExpress DSDK 链接到我自己的应用程序中.我使用 gcc 以便我们可以针对平台进行编译.在 Linux 和 Mac 上,这很容易工作:它们提供了一个静态库 (libltidsdk.a) 和头文件,我们所要做的就是使用它们.

I'm trying to link the LizardTech GeoExpress DSDK into my own application. I use gcc so that we can compile on for platforms. On Linux and Mac this works easily: they provide a static library (libltidsdk.a) and headers and all that we have to do is use them.

为 Windows 编译并不那么容易.他们使用 Microsoft Visual Studio 构建了库,我们使用 MinGW.我已阅读 MinGW 常见问题解答,但遇到了以下问题.该库全是 C++,所以我的第一个问题是:这甚至可能吗?

Compiling for windows isn't so easy. They've built the library using Microsoft Visual Studio, and we use MinGW. I've read the MinGW FAQ, and I'm running into the problems below. The library is all C++, so my first question: is this even possible?

仅根据提供的 dll 链接会产生所有 C++ 调用(构造函数、析构函数、方法等)的未定义引用"错误.

Just linking against the dll as provided yields "undefined reference" errors for all of the C++ calls (constructors, desctructors, methods, etc).

基于 MinGW 维基:http://www.mingw.org/wiki/MSVC%5Fand%5FMinGW%5FDLLs我应该能够使用实用程序 reimp 将 .lib 转换为可用的东西.我已经尝试了 LizardTech 提供的所有 .lib 文件,它们都给出了无效或损坏的导入库".我已经尝试了 reimp 实用程序的 0.4 和 0.3 版.

Based on the MinGW Wiki: http://www.mingw.org/wiki/MSVC%5Fand%5FMinGW%5FDLLs I should be able to use the utility reimp to convert a .lib into something useable. I've tried all of the .lib files provided by LizardTech, and they all give "invalid or corrupt import library". I've tried both version 0.4 and 0.3 of the reimp utility.

使用 wiki 中描述的第二种方法,我在 dll 上运行 pexport 和 dlltool 以获取 .a 存档,但这会产生相同的未定义引用.

Using the second method described in the wiki, I've run pexport and dlltool over the dll to get a .a archive, but that produces the same undefined references.

顺便说一句:我已经阅读了下面的讨论.关于这是否可行,它留下了一些歧义,鉴于 MinGW Wiki 页面,这似乎应该是可行的.如果这是不可能的,那就是我需要知道的全部.如果可以的话,我想知道如何才能做到这一点.

BTW: I have read the discussion below. It left some ambiguity as to whether this is possible, and given the MinGW Wiki page it seems like this should be doable. If it is impossible, that's all I need to know. If it can be done, I'd like to know how I can get this to happen.

如何从g++链接到VS2008生成的.libs

谢谢!

推荐答案

你不能这样做.他们从他们的 dll 中导出了 C++ 类,而不是 C 函数.不同之处在于,C++ 函数总是以特定于特定编译器版本的重整形式导出名称.

You can't do this. They have exported C++ classes from their dll, rather than C-functions. The difference is, c++ functions are always exported with names in a mangled form that is specific to a particular version of the compiler.

他们的 dll 只能以这种形式被 msvc 使用,甚至可能无法在不同版本的 msvc 之间工作,因为微软之前已经改变了他们的修改方案.

Their dll is usable by msvc only in that form, and will probably not even work between different versions of msvc, as Microsoft have changed their mangling scheme before.

如果你有任何筹码,你需要让他们改变他们的邪恶方式.否则,您将需要使用 MSVC 编写一个 shim dll,它将导入所有类,并通过返回接口的 c 函数重新导出它们.

If you have any leverage, you need to get them to change their evil ways. Otherwise you will need to use MSVC to write a shim dll, that will import all the classes, and re-export them via c functions that return interfaces.

相关文章