MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: LibraryLink & MinGW

  • To: mathgroup at
  • Subject: [mg127020] Re: LibraryLink & MinGW
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at>
  • Date: Sun, 24 Jun 2012 04:26:29 -0400 (EDT)
  • Delivered-to:
  • References: <js3uag$um$>

On Sat, 23 Jun 2012 09:21:36 +0100, fortgriff <griff at> wrote:

> Does anyone have experience with LibraryLink and MinGW?
> Using LibraryLink, I am able to load functions into Mathematica from
> the shipped version of demo.dll.  From the source file demo.c, I
> generate my own demogcc.dll (using MinGW).  Using "objdump -f
> xxx.dll", I cannot tell the difference between the two dlls.  Both
> export what they should and both look like dlls.
> Inside Mathematica, I cannot load functions from demogcc.dll.  The
> $LibraryError I get is
> Library load error 193: %1 is not a valid Win32 application.
> There are many command line options for the compiler and linker.
> Hopefully, there is a combination that works.
> Thank you for your help.

I don't know if there's enough information here to make a diagnosis, but I  
use MinGW-w64 with Mathematica and have had no problems with LibraryLink.  
That is, of course, after modifying the CCompilerDriver` package to  
support MinGW-w64 and regenerating the necessary import libraries in GNU  
COFF format. As I understand it the 32-bit MinGW should be able to link  
against MS COFF format import libraries, so I would have thought it would  
work "out of the box". However, I've never actually tried 32-bit MinGW so  
there may be some unforeseen problems. I will just describe what I did  
with MinGW-w64 and hopefully you can interpolate to your situation (or  
just copy what I did).

I'm using TDM's MinGW-w64 build which you can download at  
<>. The actual executables in this  
package are all 32-bit and as this is a multilib build it's perfectly  
allowable to use this on either 32- or 64-bit systems. You do of course  
have to make sure that the word length of your compiled libraries matches  
that of the version of Mathematica you want to load them into.

To produce the GNU COFF import libraries, you can use gendef from the  
MinGW-w64 tools (which you'll have to download separately from  
<> as it isn't included in  
TDM-GCC). Just compile that:

gcc -O -Wall -I. gendef.c gendef_def.c compat_string.c fsredir.c -m32 -o  

Now you can run:

gendef ml32i3.dll ml64i3.dll
dlltool -d ml32i3.def -l libml32i3m.a -k -m i386        -f--32
dlltool -d ml64i3.def -l libml64i3m.a    -m i386:x86-64 -f--64
del ml32i3.def ml64i3.def

where ml32i3.dll is from  
SystemFiles\Links\MathLink\DeveloperKit\Windows\SystemAdditions and  
ml64i3.dll is from  
Then copy the new import libraries (libml32i3m.a and libml64i3m.a) to  
their proper locations and you're done.

You'll need to do the same thing for WolframRTL.dll and  
WolframRTL_Minimal.dll, which are found in SystemFiles\Libraries\Windows  
(32-bit versions) and SystemFiles\Libraries\Windows-x86-64 (64-bit), to  
produce libWolframRTL.a and libWolframRTL_Minimal.a. If you want the  
static libraries too, WolframRTL_Static_Minimal.a can be obtained from  
WolframRTL_Static_Minimal.lib just by copying it under the new name and  
running ranlib on it (no need for gendef or dlltool in this case).

After you've done this you should have all the necessary 32- and 64-bit  
GNU COFF import libraries in the right places. Next you need to look at  
the CCompilerDriver` package under AddOns\Applications\CCompilerDriver.  
The files needing modifications are CCompilerDriverRegistry.m and  
MinGWCompiler.m. I won't describe the modifications here because strictly  
speaking modifying these files contravenes Mathematica's licence, so all  
the standard legal mumbo-jumbo applies; do it only if this aspect of the  
licence is not legally enforceable in your jurisdiction, etc. etc. Suffice  
to say the necessary changes are quite minimal and should be fairly  

Now you should be able to compile the LibraryLink examples without a  


(* Compile the 32-bit library: *)
CreateLibrary[{"C:\\Program Files\\Wolfram  
"demo_mathlink_32", "TargetSystemID" -> "Windows", "ShellOutputFunction"  
-> Print];

(* Now the 64-bit one: *)
CreateLibrary[{"C:\\Program Files\\Wolfram  
"demo_mathlink_64", "TargetSystemID" -> "Windows-x86-64",  
"ShellOutputFunction" -> Print];

(* If you have a 32-bit system: *)
funReverse = LibraryFunctionLoad["demo_mathlink_32", "reverseString",  
LinkObject, LinkObject];

(* Or on 64-bit Windows: *)
funReverse = LibraryFunctionLoad["demo_mathlink_64", "reverseString",  
LinkObject, LinkObject];

funReverse["a nut for a jar of tuna"]
(* -> "anut fo raj a rof tun a" *)

Obviously if you get any errors in the compiler output then fix them and  
try again. If you did everything correctly, you should now have MinGW-w64  
fully working with CCompilerDriver`, LibraryLink`, etc.

  • Prev by Date: Combining Two Lists
  • Next by Date: Re: Default path for opening files
  • Previous by thread: LibraryLink & MinGW
  • Next by thread: Re: LibraryLink & MinGW