MinGW shall compile a shared C library mylib
that contains functions like
declspec(dllexport) int foo();
The library shall be used in a C++ application under Visual Studio.
Building the library (under CMake, with option GNUtoMS
) yields three files, mylib.dll mylib.dll.a mylinb.lib
. To inspect the latter,
dumpbin /HEADERS mylib.lib
prints one stance per exported function. The stance for the above function foo
contains the line
Symbol name : _foo
So, MinGW does not generate the prefix _imp_
. Expectedly, linking the dependent application fails because Visual Studio cannot find _imp_foo
.
How to get this right?
Prefer the fork MinGW-w64 over the original MinGW
Instead of
mingw.exe
from http://MinGW.org,
use
x86_64-w64-mingw32-gcc
from http://www.msys2.org.
With this, dumpbin.exe
finds not
Symbol name : _foo
but
Symbol name : foo
and Visual Studio succeeds in linking libmylib.lib
.
For background on mingw vs msys2/mingw-w64, see Wikipedia: "In ... 2013 a new project was started, MSYS2 together with 32-bit and 64-bit MinGW packages. This project was created to keep track with newer advances of the Cygwin project ... MSYS2 is an independent rewrite of MSYS, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software."
MinGW is a registered trademark licensed to the MinGW project. The names of the compilers in the MSYS2 project are somewhat confusing: Occasionally they are called MinGW for short; the executables have mingw32
in their name even when dedicated to 64 bits; the official name seems to be MinGW-w64.
Thanks to @jacob whose use of MinGW-w64
in his answer provided the decisive cue.