Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
I'm using Mathematica 9. –  Loi.Luu Oct 25 '13 at 13:26
add comment

1 Answer 1

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$ 
share|improve this answer
    
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++ –  Loi.Luu Oct 26 '13 at 10:00
    
@Loi.Luu When you try my code above, which uses the automatic compilation from within Mathematica, does the In[2]:= appear too? –  halirutan Oct 26 '13 at 13:39
    
Actually I'm not sure how to set the TargetDirectory and Libraries correctly. I paste your script into Mathematica console and nothing happens. –  Loi.Luu Oct 26 '13 at 18:13
    
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. –  halirutan Oct 26 '13 at 18:36
    
I still get this error, I mean the In[2]:= still appears.. –  Loi.Luu Oct 26 '13 at 20:01
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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