Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10608 > unrolled thread

LibraryLink & MinGW

Started byfortgriff <griff@eigenpt.com>
First post2012-06-23 08:21 +0000
Last post2012-06-29 08:49 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  LibraryLink & MinGW fortgriff <griff@eigenpt.com> - 2012-06-23 08:21 +0000
    Re: LibraryLink & MinGW "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-06-24 08:25 +0000
      Re: LibraryLink & MinGW Mike Griffis <griff@eigenpt.com> - 2012-06-28 08:07 +0000
    Re: LibraryLink & MinGW Michael Weyrauch <michael.weyrauch@gmx.de> - 2012-06-26 08:50 +0000
      Re: LibraryLink & MinGW Mike Griffis <griff@eigenpt.com> - 2012-06-28 08:07 +0000
        Re: LibraryLink & MinGW Michael Weyrauch <michael.weyrauch@gmx.de> - 2012-06-29 08:49 +0000

#10608 — LibraryLink & MinGW

Fromfortgriff <griff@eigenpt.com>
Date2012-06-23 08:21 +0000
SubjectLibraryLink & MinGW
Message-ID<js3uag$um$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#10617

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-06-24 08:25 +0000
Message-ID<js6itj$86m$1@smc.vnet.net>
In reply to#10608
On Sat, 23 Jun 2012 09:21:36 +0100, fortgriff <griff@eigenpt.com> 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.
> MWG
>

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.

[toc] | [prev] | [next] | [standalone]


#10672

FromMike Griffis <griff@eigenpt.com>
Date2012-06-28 08:07 +0000
Message-ID<jsh3c6$sjd$1@smc.vnet.net>
In reply to#10617
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.

[toc] | [prev] | [next] | [standalone]


#10636

FromMichael Weyrauch <michael.weyrauch@gmx.de>
Date2012-06-26 08:50 +0000
Message-ID<jsbt5h$6qc$1@smc.vnet.net>
In reply to#10608
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

Am 23.06.2012 10:21, schrieb fortgriff:
> 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
>


[toc] | [prev] | [next] | [standalone]


#10677

FromMike Griffis <griff@eigenpt.com>
Date2012-06-28 08:07 +0000
Message-ID<jsh3b7$sj0$1@smc.vnet.net>
In reply to#10636
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.)

[toc] | [prev] | [next] | [standalone]


#10681

FromMichael Weyrauch <michael.weyrauch@gmx.de>
Date2012-06-29 08:49 +0000
Message-ID<jsjq70$cuj$1@smc.vnet.net>
In reply to#10677
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

Am 28.6.2012 10:07, schrieb Mike Griffis:
> 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.)
>


[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web