Linking to Msvc Dll from Mingw

Linking to MSVC DLL from MinGW

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.

Link a MSVC compiled DLL in a MinGW-built project

You can only link MSVC compiled C DLLs with MinGW, and only on 32-bit Windows. The MinGW linker can link directly to the DLL (if the functions are properly exported and not only available through an import library) or the usual import library. See here and here for how to generate a MinGW import library from a DLL.

You'll do it just like with MSVC (compile the dll with the functions marked dllexport, and compile the code using the dll with the functions marked dllimport, or use a .def file or something). Remember you need to export C functions, which means they need to be marked extern "C".

I would strongly suggest though, making the code compatible with MinGW, and just compile everything with that. Or use the MSVC version of Qt.

Linking a MinGW library to a MSVC app with a C interface

I figured out what works for me, so I'll share.

I was not able to link a static library between compilers as I originally attempted. My understanding is that the extra info kept in the lib to allow link-time code generation is compiler-specific. Brecht Sanders's answer outlines a few possible reasons why the code wouldn't be compatible.

I was, however, able to link to a shared library, with a few extra steps.

Using the same suite (see the question), I compiled as shared and got libopenal.dll, libopenal.dll.a, and libopenal.def. In my case, the .def file was generated by the suite. Accoding to this answer, you can generate a .def file with gcc using:

gcc -shared -o your_dll.dll your_dll_src.c -Wl,--output-def,your_dll.def

Trying to link to libopenal.dll.a still gave me errors (I don't know exactly why, and I already discarded the logs.) What I did instead was generate a .lib file from the .def file. In Visual Studio's built-in terminal:

lib /machine:x64 /def:libopenal.def

This generated a libopenal.lib file in the working directory. Linking to this file worked perfectly, and I was able to successfully load the dll at runtime.

I've tested this same method with many other MinGW-compiled libraries from the suite, including libavformat, libavcodec, libavutil, libavdevice, swresample, and swscale, and thus far all of them have worked.

Kind of convoluted, but it seems to work well for me, so I hope this helps anyone else with the same problem.

MinGW linking with MSVC's dll library trouble (undefined references)

No. The MSVC name mangling is proprietary and undocumented.

Even if you could get the name mangling consistent across compilers, there are tons of other little differences that would make your linked code crash in all sorts of weird places (class layout, function calls, etc...).

If you need interoperability, write a C interface. 32-bit MinGW(-w64) GCC will even link to 32-bit MSVC C libraries and vice versa (if you include the necessary compiler libraries like libgcc or the security checking cookie thingie library MSVC adds to its code generation by default).

How to use libraries compiled with MingW in MSVC?

Based on this error you put in a comment:

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

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

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

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@.

Do libraries compiled with MinGW work with MSVC?

No, libraries compiled on MinGW can't be used with MSVC. The main reasons are:

  • Lack of ABI compatibility. That is to say that in binary, the way things can be laid out are different depending on the compiler.
  • Difference in how the standard library is implemented. The standard library can be implemented in many ways. The C++ Standard just says how functions should behave and does not force compiler developers to implement them in the same way.

In general, things compiled with one compiler aren't compatible with another compiler.



Related Topics



Leave a reply



Submit