Message Boards Message Boards

0
|
9250 Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Linking my own dll library to Mathematica

Posted 10 years ago
I tried to create my own C function, compile as a dll and link it to Mathematica.

For my first steps I used an example from the help, but it was wrong...
My C code is 
 #include "c:\Program Files\Wolfram Research\Mathematica\9.0\SystemFiles\IncludeFiles\C\WolframLibrary.h"
 
 DLLEXPORT mint WolframLibrary_getVersion( )
 {
     return WolframLibraryVersion;
 }
 
 DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData)
 {
    return 0;
}


DLLEXPORT int demo_I_I(WolframLibraryData libData,

mint Argc, MArgument *Args, MArgument Res) {
mint I0;
mint I1;
I0 = MArgument_getInteger(Args[0]);
I1 = I0 + 1;
MArgument_setInteger(Res, I1);
return LIBRARY_NO_ERROR;
}
After compilation (using Codeblocs and mingw32-g++.exe) with 0 errors and 0 warnigs the result is a .dll file called knihovna-1.dll

In Mathematica is my code
In[14]:= FindLibrary["C:\\Users\\Tom\\Dropbox\\MathematicaC\\knihovna-1\\bin\\knihovna-1"]
Out[14]= "C:\\Users\\Tom\\Dropbox\\MathematicaC\\knihovna-1\\bin\\knihovna-1.dll"
In[15]:= LibraryFunctionLoad["C:\\Users\\Tom\\Dropbox\\MathematicaC\\knihovna-1\\bin\\knihovna-1", "demo_I_I", {Integer}, Integer]
In[15]:= LibraryFunction::libload: The function demo_I_I was not loaded from the file C:\Users\Tom\Dropbox\MathematicaC\knihovna-1\bin\knihovna-1.dll. >>
Out[15]= $Failed

I seems, my poor undestandig how to link a library is not poor, but totaly wrong...

Please, it is possible to tell me shortly, what I am doing wrong and ho to make i correct?

Thanks!
POSTED BY: Tomáš Hruš
6 Replies

using Codeblocs and mingw32-g++.exe

There's the problem. This is C code, but you compiled it with a C++ compiler, which produces different symbols than what Mathematica is trying to load from the shared library.

Put EXTERN_C before each function (where you already have DLLEXPORT). This will expand to extern "C" in C++ mode, which is what is needed to make this work.

POSTED BY: Szabolcs Horvát
It's usually less trouble if you install Microsoft's compiler.  It's possible to install only the command line tools, not the IDE, and it'll work out of the box with Mathematica.  Also, don't compile using your IDE.  Use CreateLibrary[] instead.  There are lots of things that can go wrong if the linking isn't done correctly and CreateLibrary will take care of it for you.

Related: http://mathematica.stackexchange.com/a/8442/12
POSTED BY: Szabolcs Horvát
Posted 10 years ago
Thank you, I will try it.
POSTED BY: Tomáš Hruš
Posted 10 years ago
So, the instalation of  Microsoft Windows SDK 7.1 failed and it dont understand why. It is too far from Mathematica for me... In this moment I dont now, what more to try.

But this is not a problem fot this dashboard, I think.

Thak you, Szabolcs.
POSTED BY: Tomáš Hruš
Is your OS 32 bit or 64 bit?  If you compile a 32 bit dll in a 64 bit operating system, LibraryLInk will fail.
POSTED BY: Frank Kampas
Posted 10 years ago
Frank: You are true, my system is 64 bit and the compiller is 32 bit.

Using mingw64 is the behaviour differrent -- the LibraryFunctionLoad remains "hanging" and after Mathematica shutdown, the kernel is still running and must be ended using Taskmanager.
POSTED BY: Tomáš Hruš
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract