I have problem when using mathlink, the source code is quite simple:

void MAXF(char* ex1, char* ex2, char** result){
   MLINK lp;
   int pkt;
   MLEnvironment env;
   env = MLInitialize(NULL);
   if (env == NULL) return;
   int argcs = 4;
   char *argvs[5] = {"-linkmode", "launch", "-linkname", "math -mathlink", NULL};
   lp = MLOpen(argcs, argvs);
   if (lp == NULL) return;

   *result = malloc(100);
   sprintf(*result, "0");

   MLPutFunction(lp, "Exit", 0L);

and in main, i call it by:

char* test;
MAXF("0", "0", &test);

However, after running, the terminal always shows this line:


I believe that there is a problem, but I can't figure it out. Please help.

  • $\begingroup$ I'm using Mathematica 9. $\endgroup$ – Loi.Luu Oct 25 '13 at 13:26

On my system, which is Ubuntu Linux 12.04 (64bit), this does not happen. I used the following code, which extends your example into a SSCCE (Short, Self Contained, Correct (Compilable), Example). The C code can be found on pastebin and you might want to adapt the setting for "TargetDirectory" and "Libraries":

<< CCompilerDriver`
src = Import["http://pastebin.com/raw.php?i=WU6JYq25", "Text"];
prog = CreateExecutable[src, "out", "ShellCommandFunction" :> Print, 
  "ShellOutputFunction" :> Print, "Libraries" -> {"ML64i3"}, 
  "TargetDirectory" -> "/tmp"]

After starting the program in the terminal I get no output

patrick@lenerd:~/tmp$ ./out 
  • $\begingroup$ I don't know why the error occurs in my system, even when I remove the two lines: *result = malloc(100); sprintf(*result, "0"); I use ubuntu 12.04 64 bit also. I compile by gcc -o main main.c -lML64i3 -lm -lpthread -lrt -lstdc++ $\endgroup$ – Loi.Luu Oct 26 '13 at 10:00
  • $\begingroup$ @Loi.Luu When you try my code above, which uses the automatic compilation from within Mathematica, does the In[2]:= appear too? $\endgroup$ – halirutan Oct 26 '13 at 13:39
  • $\begingroup$ Actually I'm not sure how to set the TargetDirectory and Libraries correctly. I paste your script into Mathematica console and nothing happens. $\endgroup$ – Loi.Luu Oct 26 '13 at 18:13
  • $\begingroup$ I have edited the code block. Just copy it completely into a Mathematica notebook and evaluate it. After it prog contains the path to the built executable which you can start by simply calling it in a terminal. In my case (now here on OSX) it is /private/tmp/out, in your case it will be something different. Just copy the output to a terminal. $\endgroup$ – halirutan Oct 26 '13 at 18:36
  • $\begingroup$ I still get this error, I mean the In[2]:= still appears.. $\endgroup$ – Loi.Luu Oct 26 '13 at 20:01

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.