3
$\begingroup$

I am trying to use WSTP to link to the addtwo.c function example that comes with Mathematica. I have compiled addtwo successfully (took an effort) and if I run it in the command line I can connect it to Mathematica and use it. However running

Install["./addtwo"]

in Mathematica just hangs until I abort. The precompiled addtwo binary that comes with Mathematica does install fine. Is there a way to find out more about why the Install[] fails?

I compiled using the (adapted) makefile which produces:

:~$ make addtwo
/usr/local/Wolfram/Mathematica/10.1/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/wsprep addtwo.tm -o addtwotm.c
gcc -c -I/usr/local/Wolfram/Mathematica/10.1/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions addtwotm.c
gcc -c -I/usr/local/Wolfram/Mathematica/10.1/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions addtwo.c
gcc -I/usr/local/Wolfram/Mathematica/10.1/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions addtwotm.o addtwo.o -L/usr/local/Wolfram/Mathematica/10.1/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions -lWSTP64i4 -lm -lpthread -lrt -lstdc++ -ldl -luuid -lWSTP64i4  -o addtwo

Note that addition of -lWSTP64i4 didn't change anything, I tried it because of this answer which was already a bit desperate.

Could it be because of my gcc version? ( 4.6.3-1ubuntu5 )

$\endgroup$
  • $\begingroup$ You can try to set up the MathLink ("WSTP") connection manually to find out at which step it fails. First run addtwo in a terminal. It should give you a "Create link:" prompt. Type the name of a link (you can make it up). Let's say you chose the name "foo". Now in Mathematica, do link = LinkConnect["foo"], then finally Install[link]. If all went well, addtwo will be "installed" now and ready to use. Otherwise you'll find out at whcih step the failure happens. $\endgroup$ – Szabolcs Dec 8 '15 at 14:03
  • $\begingroup$ I may be wrong about this, but I doubt it's your gcc version. I tested compiling MathLink executables with gcc 4.6 and M 10.2 the other day. It worked. $\endgroup$ – Szabolcs Dec 8 '15 at 14:07
  • $\begingroup$ BTW just in case, try the MathLink version as well instead of WSTP. It's the very same thing, just renamed. $\endgroup$ – Szabolcs Dec 8 '15 at 14:12
  • $\begingroup$ See also this answer. $\endgroup$ – ilian Dec 8 '15 at 15:24
  • $\begingroup$ @Szabolcs Wow! Fast! I tried the two step version you suggested and it worked. Just doing it without running addtwo in a terminal doesn't. However I tried MathLink (I was not optimistic and a bit annoyed maybe...) but it works! I used the same makefile, just replaced the WSTPLINKDIR, the WSTPLIB, and set WSPREP to the mprep executable. So either the library or the preparation file is broken I guess. $\endgroup$ – tortortor Dec 8 '15 at 15:25
2
$\begingroup$

Summarizing the comments into an answer: this is really the same issue as in WSTP hangs in Mathematica 10.2.

If using dynamic linking (-lWSTP64i4 will pick up the .so library by default), then said shared library should be copied to a systemwide location or put on LD_LIBRARY_PATH.

The prebuilt examples do not exhibit this behavior because they are statically linked. The same can be accomplished by using -L${LIBDIR} libWSTP64i4.a in your link line.

If one uses the MathLink examples instead, they will work without additional requirements even if dynamically linked. The reason is that when Mathematica is started, it adds to LD_LIBRARY_PATH the $InstallationDirectory/SystemFiles/Libraries/$SystemID directory, which has a copy of the corresponding MathLink libraries, libML32i4.so and libML64i4.so.

$\endgroup$
  • $\begingroup$ Thanks for putting up an answer! Maybe add that just using MathLink instead of WSTP also solves the problem? For amateurs like me it seems like the easiest solution in hindsight. Of course sooner or later getting to grips with dynamic and static linking etc. will probably be necessary. But maybe someone just needs the whole linking thing once for one C function and then there is no need to get into compiling details. $\endgroup$ – tortortor Dec 12 '15 at 5:23
  • $\begingroup$ I actually have the directory with -lWSTP64i4 on my LD_LIBRARY_PATH (at least in .bashrc which might not be "global"?) but that is not enough to make it work. So I think it should be mentioned that LD_LIBRARY_PATH should be set inside Mathematica. Or somewhere where Mathematica is affected wherever that may be... $\endgroup$ – tortortor Dec 12 '15 at 5:35
  • $\begingroup$ @tortortor Added a note about MathLink. Regarding .bashrc, the setting there would work, provided Mathematica is actually started from a shell that has this variable set. But in the case when the parent process is different, you would need to set LD_LIBRARY_PATH in an appropriate way for your OS, so it can be inherited properly. $\endgroup$ – ilian Dec 12 '15 at 6:05
  • $\begingroup$ Perhaps ~/.profile is a good place for a persistent setting. In any case, how environment variables work is not really a Mathematica issue, though SetEnvironment is convenient to use within a Mathematica session. $\endgroup$ – ilian Dec 12 '15 at 6:19

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.