Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LibraryLink & MinGW

230 views
Skip to first unread message

fortgriff

unread,
Jun 23, 2012, 4:21:36 AM6/23/12
to
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.
MWG

Oleksandr Rasputinov

unread,
Jun 24, 2012, 4:25:23 AM6/24/12
to
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
<http://tdm-gcc.tdragon.net/download>. 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
<http://sourceforge.net/projects/mingw-w64/> 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
gendef.exe

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
SystemFiles\Links\MathLink\DeveloperKit\Windows-x86-64\SystemAdditions.
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
obvious.

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

Needs["CCompilerDriver`"];

(* Compile the 32-bit library: *)
CreateLibrary[{"C:\\Program Files\\Wolfram
Research\\Mathematica\\8.0\\SystemFiles\\Links\\LibraryLink\\LibraryResources\\Source\\demo_mathlink.c"},
"demo_mathlink_32", "TargetSystemID" -> "Windows", "ShellOutputFunction"
-> Print];

(* Now the 64-bit one: *)
CreateLibrary[{"C:\\Program Files\\Wolfram
Research\\Mathematica\\8.0\\SystemFiles\\Links\\LibraryLink\\LibraryResources\\Source\\demo_mathlink.c"},
"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.

Michael Weyrauch

unread,
Jun 26, 2012, 4:50:57 AM6/26/12
to
I have had no problems to generate .dll's using MinGW
(significantly more complicated than the demos)

So, I would suggest you post exactly the commands you
used in order to produce the .dlls and then load the functions
into Mathematica, and I may be able
to tell you what is going wrong.

Michael

Mike Griffis

unread,
Jun 28, 2012, 4:07:34 AM6/28/12
to
Thank you for your comments. I think my issue is using win32 port of mingw. The website mingw.org only gets you win32 port, it seems. I wasn't aware there was a win64 port. Thanks for the heads up.

Mike Griffis

unread,
Jun 28, 2012, 4:07:03 AM6/28/12
to
When reviewing these comments, I started thinking my issue is using win32 port of mingw. Are you using win64? Where are you downloading your mingw? I think mingw.org only gets you a win32 port. (My gcc commands were minimal to compile and create a "shared" library.)

Michael Weyrauch

unread,
Jun 29, 2012, 4:49:36 AM6/29/12
to

Well, unlike Alexander Rasputinov, I am using the 32 bit implementation,
and as he suspected, in this case everything works "out of the box"
indeed. I downloaded the the MinGW from its official website.
But, of course, in order to create the .dll I do not need to use gcc
and all that directly but everything is taken care of "out of the box"
by the appropriate Mathematica commands as e.g. CreateLibrary[].
(Of course, if you need any special libraries for your project you
need to tell this in the CreateLibrary[] command as documented. So, e.g.
I am linking in some code which was written in Fortran so I explicitely
have to load the standard Fortran libraries)

The only small issue I encountered was the fact that in order that
everthing really works "out of the box" you MUST install MinGW in
its default directory, which is "C:\MinGW". If you install it e.g. on
another partition, the Mathematica interface to the Compiler does not
find it. This restriction can be circumvented, but then you must change
some Mathematica files, which I do not recommend. (Actually I find it
rather strange that the carfully designed Mathematica interface to the
various compilers has such a restriction, which could be easily avoided
to my opinion.)

Michael
0 new messages