I've managed to reduce my problem to a very simple C program:
#include "mathlink.h"
#include "WolframLibrary.h"
int SetX( WolframLibraryData libData )
{
int errorCode;
int nextPacket;
MLINK link = libData->getMathLink( libData );
MLPutFunction( link, "EvaluatePacket", 1 );
MLPutFunction( link, "Set", 2 );
MLPutSymbol( link, "x" );
MLPutDouble( link, 3.14159 );
errorCode = libData->processMathLink( link );
if ( errorCode != 0 )
return errorCode;
nextPacket = MLNextPacket( link );
if ( nextPacket == RETURNPKT )
MLNewPacket( link );
return 0;
}
DLLEXPORT int WolframLibrary_getVersion()
{
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData)
{
return 0;
}
DLLEXPORT int testIt(WolframLibraryData libData, mint argc, MArgument*
margs, MArgument result)
{
int error = SetX( libData );
if ( error != 0 ) {
libData->Message( "error setting x" );
}
return error;
}
I use AppendTo
to add the directory containing the compiled
shared object/Dll to $LibraryPath
, then do:
In[4]:= LibraryFunctionLoad[ "CB", "testIt", {}, Void ]
Out[4]= LibraryFunction[ <>, testIt, {}, {}]
In[5]:= %[]
During evaluation of In[5]:= LibraryFunction::error setting x: -- Message text not found -- >>
During evaluation of In[5]:= LibraryFunction::typerr: An error caused by inconsistent types was encountered evaluating the function testIt. >>
Out[5]= LibraryFunctionError[LIBRARY_TYPE_ERROR,1]
Regretfully, the error message doesn't tell me what type is
wrong, and what type I should be using instead. (I've tried
using MLPutString
, insteamd of MLPutSymbol
, in the above,
with the same results.)
I've tried this with the same results under Windows and Linux, so I don't think it's a problem with the build procedure I'm using; but FWIW, here is the command line used to build the shared object under Linux:
Gabi (41): echo $MATHINCL
-I /usr/local/Wolfram/Mathematica/8.0/SystemFiles/IncludeFiles/C -I /usr/local/Wolfram/Mathematica/8.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions
Gabi (42): echo $MATHLIB
/usr/local/Wolfram/Mathematica/8.0/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so
Gabi (43): gcc -shared -fPIC $MATHINCL CB.c $MATHLIB -o CB.so
(I use something similar from CygWin bash under Windows, with options /MDd and /Tc.)