Question

I am using MinGW64 (Windows 7) without MSYS and I have the following problem:

I have one dll, written in C99, which has to have the .mexw64 suffix so it can be used by Matlab. I would like to be able to link this dll form another dll (mexw64) dynamically but gcc won't allow me to link directly. I cannot do static linking, because both dlls have many functions of the same name which can be hidden by not exporting their symbols when creating the shared library.

So far, I have tried:

  1. To create a symbolic link (with correct suffix and preffix) using mklink. This works, but I was not able to run mklink from the makefile. Maybe it is due to the fact I am not using MSYS which could have ln -s (I havent checked).
  2. To do the copy of the first dll and correcting the suffix and prefix. This worked better than I expected, because on runtime the second dll actually uses the original .mexw64 and not the dll copy. I guess it is just because the .mexw64 is found first, but why is that .mexw64 searched in the first place? How the system know it is actually a dll?

My question is, is this correct/safe enough? Are there any other options?

Thanks for comments.

Was it helpful?

Solution

You should build a proper implib, either as a linker output or from a .def.

Linker:

$ gcc -shared -o testimpl.mexw64 testimpl.c -Wl,--out-implib,libtestimpl.a
$ dlltool -I libtestimpl.a 
testimpl.mexw64

Or create a .def file, specifying an explicit LIBRARY:

$ cat testimpl.def
LIBRARY testimpl.mexw64
EXPORTS
    test @1
$ dlltool -d testimpl.def -l libtestimpl.a
$ dlltool -I libtestimpl.a 
testimpl.mexw64

And finally, link stuff:

$ gcc -o test.exe test.c libtestimpl.a
# or
$ gcc -o test.exe test.c -L. -ltestimpl

$ grep testimpl.mexw64 test.exe
Binary file test.exe matches
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top