# What is causing a type error when trying to modify Mathematica state from a function linked with LibraryLink

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;
if ( errorCode != 0 )
return errorCode;
if ( nextPacket == RETURNPKT )
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
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.)

-
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.
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.) –  James Kanze Dec 27 '12 at 12:52