1
$\begingroup$

I am trying to use C++ programs through MathLink in my notebooks, but I cannot compile successfully the simple programs included in Mathematica.

I do not have a specific question, I am just looking for guidance.

$Version $SystemID "9.0 for Linux x86 (64-bit) (November 20, 2012)" "Linux-x86-64"

My operating system is Linux Mint 17.3 Cinnamon 64-bit.

Step by step, what I am trying to do is the following:

cd $InstallationDirectory/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/ mcc -o addtwo ../MathLinkExamples/addtwo.c ../MathLinkExamples/addtwo.tm

Since I am trying to compile from the directory where the libraries and header (mathlink.h) is, I think it should work (I am no C expert either). I've also tried copying the addtwo.c and addtwo.tm files in the "CompilerAdditions" folder and run everything from there with the same results. I get the following errors

$InstallationDirectory/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i3.so: undefined reference to 'shm_open' (*more undefined references, see below*) collect2: error: ld returned 1 exit status

Undefined references to: 'sem_init', 'sem_unlink', 'sem_close', 'pthread_sigmask', 'sem_destroy', 'shm_unlink', 'pthread_create', 'sem_post', 'sem_trywait', 'sem_open', 'sem_wait', and 'pthread_join'. It seems related to semaphore, but I am really clueless here.

$\endgroup$
2
$\begingroup$

The easiest way to see what libraries are required is to let Mathematica compile it and look at the commandline. First, locate the two required files addtwo.tm and addtwo.c. This might be a bit different on your system:

file = FileNames["addtwo.*", {$InstallationDirectory}, Infinity][[5 ;; 6]]

(* {"/usr/local/Wolfram/Mathematica/10.3.1/SystemFiles/Links/\
MathLink/DeveloperKit/Linux-x86-64/MathLinkExamples/addtwo.c", \
"/usr/local/Wolfram/Mathematica/10.3.1/SystemFiles/Links/MathLink/\
DeveloperKit/Linux-x86-64/MathLinkExamples/addtwo.tm"} *)

After that, just try:

Needs["CCompilerDriver`"];
CreateExecutable[file, "addTwo", "ShellOutputFunction" :> Print, 
 "ShellCommandFunction" :> Print]

and you see (1) whether it succeeds and (2) which libraries are linked. Here, it is:

-l"ML64i4" -lm -lpthread -lrt -lstdc++ -ldl -luuid
$\endgroup$
  • $\begingroup$ Thank you. It does work and it does include other directories (and libraries) for the gcc compilation as well which I did not. I think I'll compile the c functions using this CreateExecutable function from now on. $\endgroup$ – user13427 May 4 '16 at 3:27
0
$\begingroup$

Without knowing specifics (haven't done what you are trying to do), it's pretty common to see those types of runtime linker errors if you don't have the correct link parameters. try adding '-lrt' (without quotes) to the end of the mcc command. Does that help?

$\endgroup$
  • $\begingroup$ I tried that, but without results. $\endgroup$ – user13427 May 4 '16 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy