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 am trying to run a Mathematica example concerning Mathematica interfacing issues with C programs. In particular I am trying to call a Mathematica function from C.

Example factor.c is the best in my case. As explained here at the Invoking the Mathematica Kernel from Within a Prebuilt Example section, I successfully build the examples (using the makefile through make all). After that I run the exaple as explained by typing:

./factor -linkmode launch -linkname 'math -mathlink'

But I get back to my shell after launching the program with no output as I did nothing. Then I try again just by launching ./factor, the program then asks me: Create link:, so I type 'math -mathlink', prfess ok, then it prompts: Integer to factor:, I type a 6 digit one and after that the program blocks (it waits for something, but after 5 minutes I kill it).

Consider that I am on Unix. My Mathematica installation is in this path /home/myuser/progs/Mathematica.... I run the example respectively with Mathematica running and not running. Same results.

How to make the example work?

Something strange

When I kill theprograms that I launch but stay on wait as I described before, it seems that they finally got killed, but it is not! When I try ps on my shell, they are there!!! Need to send SIGKILL to definetely get rid of them...

About links...

I bumped into a Mathematica function called LinkCreate. Maybe I need to run Mathematica, open a link from the interface, and then launch the example? I am trying some, so far no results, will keep on doing so...

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I'm not sure why it doesn't work for you. Try using the full path to the kernel executable. The following works for me on OS X:

./factor -LinkMode Launch -LinkName '/Applications/Mathematica\ 9.app/Contents/MacOS/MathKernel'

I do have a symlink to MathKernel in a location that's in the PATH, but if I use that, as in ./factor -LinkMode Launch -LinkName math, it doesn't work here either. ./factor -LinkMode Launch -LinkName ~/bin/math (i.e. giving the full path) does work.


If you want to start Mathematica separately (e.g. for understanding better what happens), do this:

Launch factor and at the Create link: prompt type any name. For this example, let's use mylink as the link name.

Then launch Mathematica in another terminal as math. Type link = LinkConnect["mylink"]. If it worked, type $ParentLink = link. Now you can use factor: type the number you want to factor and press enter.

share|improve this answer
    
You are my savior... did not know it was possible to do this way. Thanks a lot, for real!!! –  Andry May 3 '13 at 18:05
add comment

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.