2
$\begingroup$

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);
   MLClose(lp);
   MLDeinitialize(env);
}

and in main, i call it by:

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

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

In[2]:= 

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

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

1 Answer 1

1
$\begingroup$

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 
patrick@lenerd:~/tmp$ 
$\endgroup$
7
  • $\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
    Commented Oct 26, 2013 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
    Commented Oct 26, 2013 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
    Commented Oct 26, 2013 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
    Commented Oct 26, 2013 at 18:36
  • $\begingroup$ I still get this error, I mean the In[2]:= still appears.. $\endgroup$
    – Loi.Luu
    Commented Oct 26, 2013 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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