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));
void add P((double));
:Function:      add
:Pattern:       addNumber[$CurrentLink, n_Real ]
:Arguments:     { n }
:ArgumentTypes: { Real }
:ReturnType:    Manual
:Function:      get
:Pattern:       getSum[ $CurrentLink ]
:Arguments:     {  }
:ArgumentTypes: {  }
:ReturnType:    Real
double sum = 0;
int count = 0;
void add(n) double n;
    sum += n*(count);
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.

  • $\begingroup$ 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 $\endgroup$ – Szabolcs Jun 3 '17 at 20:50
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Jun 3 '17 at 20:54
  • $\begingroup$ 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. $\endgroup$ – Szabolcs Jun 3 '17 at 21:02
  • $\begingroup$ 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. $\endgroup$ – Niclas Jun 3 '17 at 22:23
  • $\begingroup$ 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. $\endgroup$ – 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 don't know, why Wolfram used a declaraton style older than C99 in the example files, they ship with Mathematica, and I thought this would matter for some reason, but it didn't, so I could change the declaration style.

| improve this answer | |
  • $\begingroup$ Which are the examples files where you see old-style declarations? What version of Mathematica are you using? $\endgroup$ – Szabolcs Jun 10 '17 at 9:34
  • $\begingroup$ I'm using Mathematica 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. $\endgroup$ – Niclas Jun 10 '17 at 14:40
  • $\begingroup$ You're right. I only looked at the ML version, i.e. files in the MathLink directory, not the WSTP one. There, everything is updated to the new style. I take this as another sign that the claim that MathLink is deprecated in favour of WSTP is utter nonsense. They still seem to care more about the ML version in internally than the WS version, not to mention that in some contexts only the ML version works ... $\endgroup$ – Szabolcs Jun 10 '17 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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