In Mathematica I can make the assignment 'foo = bar' with Set[foo,bar]. However, if I try to evaluate 'Set[foo,bar]' through MathLink then it appears that MathLink gets stuck while trying to read additional information from the link. Here's a small sample program, "myassignment.tm" :
void myAssignment P(( ));
:Begin:
:Function: myAssignment
:Pattern: myAssignment[ ]
:Arguments: { }
:ArgumentTypes: { }
:ReturnType: Manual
:End:
#include "mathlink.h"
#include <stdio.h>
void myAssignment()
{
MLPutFunction( stdlink, "EvaluatePacket", 1 );
MLPutFunction( stdlink, "Set", 2 );
MLPutSymbol( stdlink, "foo" );
MLPutSymbol( stdlink, "bar" );
MLEndPacket( stdlink );
MLNextPacket( stdlink ); /* Get return packet */
MLNewPacket( stdlink ); /* Discard return value, which is simply 'bar' */
}
int main(int argc, char *argv[]) { return MLMain(argc, argv); }
I have kept the sample program here short and concise. Adding error checking for every MathLink statement and printing out all the packets received after the assignment and their content doesn't reveal any additional information. It appears to hang after the last MLNewPacket statement has been executed. I could possibly install a yield-function, but I'd much rather understand why Mathematica is blocking at this point. Does anyone have any insights into this ?
Edit : Ultimately my goal is to assign default values to optional arguments of a function. Hence I want something equivalent to this :
Options[ foo ] = { bar->42 };
The actual name of the optional argument is not bar and the default value is not 42. Instead both are obtained by a call into a C library, which is why all of this has to happen through MathLink. Assuming I solve the assignment problem described above I can set the default value(s) by creating the list of rule(s) and the assignment through MathLink.
I'd like these default values to be put in place when the program is installed, so I would also add this to my template :
:Evaluate: myAssignment[];
Alternatively I can avoid the assignment in MathLink by just creating the list of rule(s) in MathLink (in which case I'll call the function myRules
) and then add this line to the template :
:Evaluate: Options[ foo ] = myRules[]
Unfortunately, none of the above solutions work for me, and it also fails with the simplification proposed by @halirutan.
MLPutFunction("Set", 2)
MLPutFunction("MessageName", 2)
MLPutSymbol("LibraryFunction")
MLPutString("argx")
MLPutString("called with
1` argument(s)2
argument(s) is expected.")`MLEndPacket()
MLNextPacket()
MLNewPacket()
MLPutFunction("Set", 2)
The lastPut
gave meMLEABORT
"a put or get was aborted before affecting the link" $\endgroup$ – Neel Basu Oct 23 '18 at 19:48