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
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.)


1 Answer 1


Looking into this, I can see that the docs are simply wrong. The processMathLink() function returns TRUE on success, FALSE otherwise, not what the docs describe. We'll make sure this gets fixed. Your program looks to be correct in all other ways.

  • $\begingroup$ Thanks. Not only does my small test work now, but I was able to get the actual code working. In the actual code, I had another small error: I'd spelled the first function "EvalutatePacket". Is there anyway to get better error detection---as far as I can see, the spelling error only made the packet a no-op. (Maybe by processing the response packet? In the actual code, I know that the head of what I'm setting should be a LibraryFunction; just checking for that would probably suffice.) $\endgroup$ Dec 27, 2012 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.