# MathLink linking error after OS X 10.9 (Mavericks) upgrade

After macosx 10.9 mavericks upgrade i have a compilation problem, you can see below. Even the examples given in /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/MathLinkExamples are not compiling anymore. It throws an error about the architecture. I also followed the solution given by MathLink compile errors, reinstalling the Xcode and command line tools. It didnt work.

I also checked the libMLi3.a for architecture and seems like it is ok

*libMLi3.a: Mach-O universal binary with 2 architectures libMLi3.a (for architecture i386): current ar archive random library libMLi3.a (for architecture x86_64): current ar archive random library*

and clang/gcc as well

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix

gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix

which are the latest.

I wonder if anyone manages to compile the MathLink examples after the 10.9 upgrade?

Here is the error message:

/Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/MathLinkExamples]:>
-lMLi3 -lstdc++ -framework Foundation -o addtwo
Undefined symbols for architecture x86_64:
"std::string::find_last_of(char const*, unsigned long, unsigned long) const", referenced from:
"std::string::find_first_of(char const*, unsigned long, unsigned long) const", referenced from:
"std::string::find(char const*, unsigned long, unsigned long) const", referenced from:
_mlipinterfacecontainer_get_domain_names in libMLi3.a(mlipinterface.c.o)
_pipe_make in libMLi3.a(_pipe.c.o)
MLDevices::MLTCPIPDevice::OldConnectServer() in libMLi3.a(mltcpip.cpp.o)
.
.
.
.
.
.
...
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

-
Just a quick comment that both MathLink and LibraryLink compilation works fine for me on 10.9 with XCode 5.0.1's command line tools. Haven't had time to look at your details yet. –  Szabolcs Oct 24 '13 at 18:50
I cannot reproduce the problem you mention, but you did not provide complete compilation steps. Please edit the question and describe precisely how you tried to compile the examples, making sure to include every step. You only showed us the linking step and while you mention the version of your clang installation, you used gcc to compile. Please include the version of the compiler you are actually using. –  Szabolcs Oct 24 '13 at 18:55
Well actually there are no many steps. Just go to the folder /Applications/Mathematica.app/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86‌​-64/MathLinkExamples there you will see MathLink folder, some prebuilt examples and MathLinkExamples folder. In that folder makefile file is already provided and you just need to type "make" to compile all. It is so easy. When you do that it complains. Therefore I have couple of other MathLink programs i wrote compiled long ago seems like they are working in Mathematica, though still couldn't compile. –  user6629 Oct 25 '13 at 4:37
Therefore surely you need to have Xcode/Command Line Tools preinstalled to compile the code. –  user6629 Oct 25 '13 at 6:27
I added the gcc version info under the question. Carefully looking at my question i think gcc works fine and compiles but clang couldn't link the MathLink objects (i guess in libMLi3.a library) and complains about the architecture. –  user6629 Oct 25 '13 at 7:23

Basically, I came to the same conclusions as user6629 and Szabolcs. This means there are two solutions for the commandline.

1. Specify -lstdc++.6 which is very weird, because on my machine libstdc++.dylib is just a link to libstdc++.6.dylib too. I traced the linker output and it really uses the correct library from /usr/lib which gives me some headache.
2. Use -stdlib=libstdc++ -lstdc++ as suggested by Szabolcs.

There is another solution: Use CreateExecutable from the <<CCompilerDriver package. Here the addtwo example (please set the TargetDirectory as you like)

Needs["CCompilerDriver"];
src = FileNames["addtwo.*", {FileNameJoin[{\$InstallationDirectory, "SystemFiles",
CreateExecutable[src, "out", "ShellOutputFunction" :> Print,
"ShellCommandFunction" :> Print, "TargetDirectory" -> "tmp"]


Looking at the compiler command reveals something even more weird

/usr/bin/gcc ... -l"MLi3" -lm -lpthread -lstdc++


Mathematica is using the exact same command which brings up the error in the terminal. This reveals that another solution is possible: Specify -mmacosx-version-min=10.4 which seems to turn on compatibility mode.

-
Or really -mmacosx-version-min = anything lower than 10.9. Is 10.4 available in a 64 bit version as well? –  Szabolcs Oct 25 '13 at 16:01
@Szabolcs It seems so. 10.4 is what is used by CreateExecutable on my machine. –  halirutan Oct 25 '13 at 18:05
I came across the same problem for CreateExecutable from CCompilerDriver package. I know how to solve this problem in the Makefile, but how to solve this problem if I want to use CreateExecutable? How to add something like -stdlib=libstdc++ -lstdc++ in it? –  Han Xiao Nov 2 at 5:43
Try the giving the option "CompileOptions" -> "-stdlib=libstdc++ -lstdc++" to CreateExecutable. You can find all options in the documentation under Details –  halirutan Nov 3 at 0:01
@halirutan Thanks for your suggestions. My problem has been solved. But a new problem arises. I can create a binary and install in Mma. But it never works. I started a new SE question. It will be great to have your suggestions on that issue. –  Han Xiao Nov 3 at 13:28

This is what I've been able to gather:

There are two implementations of the standard C++ library available on OS X: libstdc++ and libc++. They are not binary compatible and libMLi3 requires libstdc++.

On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. To ensure compatibility with libMLi3, we need to choose libstdc++ manually.

To do this, add -stdlib=libstdc++ to the linking command.

Note: the Makefile supplied with the MathLink examples uses the c++ command for linking (not cc or ld). In this case the -lstdc++ option can be removed from the linking command. However if you use cc for linking, then it's necessary to specify both of -stdlib=libstdc++ -lstdc++.

-
Are you sure this works for you on 10.9? Here, it is crucial that I explicitly specify -lstdc++ too. –  halirutan Oct 25 '13 at 15:20
@halirutan It was working for me because I was linking with c++ instead of cc because I modified the original example makefile. I updated the answer now. –  Szabolcs Oct 25 '13 at 15:25

I have found the solution: libMLi3 wasn't the problem but the standard libstdc++. If you change the -lstdc++ to -lstdc++.6 in makefile given in MathLinkExamples folder it compiles w/o problem. I also checked my /usr/lib folder and seems like the link file is present (/usr/lib/libstdc++.dylib -> libstdc++.6.dylib) and points the correct library but linker got complains some how.

-