# WSTP simple function doesn't finish evaluating

I'm new to WSTP so I tried to write a small c program, similar to one of the examples in the developerkit and tried to execute the functions in Mathematica. I'm using Ubuntu 16.04 64bit by the way. I wrote a template file and compiled it using wscc. This is the .tm file:

#include "wstp.h"
double get P(( void));
:Begin:
:Pattern:       addNumber[$CurrentLink, n_Real ] :Arguments: { n } :ArgumentTypes: { Real } :ReturnType: Manual :End: :Begin: :Function: get :Pattern: getSum[$CurrentLink ]
:Arguments:     {  }
:ArgumentTypes: {  }
:ReturnType:    Real
:End:
double sum = 0;
int count = 0;
{
count++;
sum += n*(count);
return;
}
double get(void){
return sum;
}
int main(argc, argv)
int argc; char* argv[];
{
return WSMain(argc, argv);
}


The code compiled without errors, but if I start the program in Mathematica using Install[] and try to execute addNumber[] with any Real value the evaluation never finishes and I'm not able to interrupt the evaluation using Abort. So I have to quit the Kernel.
So where is the problem evaluating this simple function? It is very similar to the one in the counter program in the example folder of the developertkit and that program worked fine.

• Why did you put $CurrentLink in the pattern? Where did you see this and what are you trying to achieve with it? I suggest you model your program on the several examples that come with Mathematica, such as addtwo – Szabolcs Jun 3 '17 at 20:50 • Also: the .tm file should have only the template, not any C code. You have a Manual return type for add, but you are not returning anything. This is why it hangs. Mathematica is waiting for a return value, which can be Null. You would need to do that with WSPutSymbol. – Szabolcs Jun 3 '17 at 20:54 • You also seem to be using the very old K&R style argument type declarations. This is deprecated since 1989. If you do not know how to program in C yet, I suggest you learn a bit of C before you attempt to use WSTP/MathLink with Mathematica. – Szabolcs Jun 3 '17 at 21:02 • The $CurrentLink was something I found in the counter.tm from the examples und I thougth it is important to address the same instance of the c program if I would run more than one of them, because the addNumber function is changing two variables within the program, so it matters which instance's function I am calling. – Niclas Jun 3 '17 at 22:23
• I now added WSPutSymbol(stdlink, '\0'); to the add function (and also changed the ancient type declaration style). Mathematica now aborts the evaluation with the message LinkObject::linkd: Unable to communicate with closed link LinkObject['/home/nk/wstptest/test1',314,6].. I thougth the connection would stay alive until I use Uninstall or something similar to close the connection. – Niclas Jun 3 '17 at 23:48

After I removed \$CurrentLink from the code and added WSPutSymbol(stdlink, "Null") to let the function return something to Mathematica, the program worked fine.
• I'm using Mathematica 11.0.1.0 and the files are located in /usr/local/Wolfram/Mathematica/11.0/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/WSTPExamples/. In all of the *.tm examples main is declared in K&R style and also AddToCounter in counter.tm. – Niclas Jun 10 '17 at 14:40