开发者

Linking to MSVC DLL from MinGW

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.

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?

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

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.

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.

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.

How to link to VS2008 generated .libs from g++

Thanks!


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.

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.

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.


I'm almost certain that you can't link C++ libraries across compilers like that. I've tried, really hard, but it was a long time ago and I don't remember the details. i think for C libraries is possible, with a lot of hacks.


Just thought I'd add my two cents here.

When you want to link against a dll in MSVC, what you do is linking against a .lib which contains function stubs that in turn calls the corresponding function in the .dll (an extra indirection with "__imp__" added to the mangled name). GCC doesn't do that, because in theory you can easily link directly to the dll if the functions are exposed by the export table. You can tell GCC in MinGW to look in the dll directly using -l . It is true that the C++ naming scheme varies across a few different versions of MSVC but not across all of them. They are consistent across most of the versions and extensions are added as new features gets implemented.

MinGW however uses the GCC name mangling scheme, that includes searching for external symbols, so it doesn't recognize the MSVC mangled names. The linker could easily solve this by remangling the external symbols to match MSVC and then check again if there is a match. Maybe this will be implemented someday. I've solved this in my own programming language in a similar way but that of course includes my own linker.

It is fully possible to provide GCC in MinGW with a .lib, simply specify it on the commandline the same way you specify the source files; gcc -o test.cpp main.cpp external.lib … This will provide the linker with the mangled names it should look for. MinGW is capable of dealing with MSVC mangled names too, it even has its own MSVC mangler/demangler in libmangle.


To use a DLL in Windows, you link against an import library (not the DLL itself). Import libraries typically have the extension .lib (just like static libraries). If you download the Windows SDK, you'll get import libraries for all of the system DLLs.

From your question, it sounds like you might have mixed up the .dll with the .lib. Are you sure reimp doesn't take a DLL as input and make a .LIB import library that's compatible with MinGW?

I just looked up with MinGW is on Wikipedia. According to that article, MinGW comes with redistributable import libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜