I am trying to follow this documentation: https://reference.wolfram.com/language/tutorial/SettingUpExternalFunctionsToBeCalledFromTheWolframLanguage.html

When I first tried to do mcc -o f.exe f.tm f.c I got an error: f.c: f.c:3:18: fatal error: wstp.h: No such file or directory. I fixed that error by #include "/usr/local/Wolfram/Mathematica/10.4/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/wstp.h" in f.c file.

Then, I got another error: cannot find -luuid , which I fixed by installing these three things : sudo apt-get install uuid-dev

sudo apt-get install libxmu-dev libxi-dev

sudo apt-get install libx11-dev

Now, I have this new error, which I don't know how to fix: f.o: In functionmain': f.c:(.text+0x30): undefined reference to WSMain' collect2: error: ld returned 1 exit status

Could someone please help me out?

Operating system info:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:    14.04
Codename:   trusty

g++ version : (Ubuntu 4.8.4-2ubuntu1~14.04.1)


2 Answers 2


In short, this is because you are mixing WSTP tools with MathLink tools. Use wscc instead of mcc and everything should work.

So what does mcc and wscc do? Installable MathLink programs are compiled in two steps.

  1. Translate the template file (.tm) to C code using mprep or wsprep.
  2. Compile both the code generated from the template and your own code to create an executable.

These steps are automated by mcc or wscc.

If you see a linker error complaining that MLMain or WSMain is missing, it means that the first step wasn't completed properly, or that you didn't compile (and link) the code generated from the template. MLMain/WSMain is automatically generated from the template file.

What's the difference between MathLink and WSTP?

Well, only the naming. They do exactly the same thing and there is no functionality difference whatsoever. The original name of the system was MathLink. This was changed to WSTP for marketing reasons when Mathematica 10 came out. At the same time they renamed all the API functions to use the WS prefix instead of the ML prefix. This means that compatibility was broken.

So now Mathematica comes with two libraries and two sets of tools for them. One is called MathLink and uses the ML prefix. The other is called WSTP and uses the WS prefix. Once again, there is absolutely no functionality difference, yet they are incompatible with each other. You cannot mix functions, headers or libraries from one with the other. All this is simply because of the renaming, which was done for marketing reasons.

If you use mcc, it will generate MLMain and not WSMain. But if you use the wstp.h header and link against -lWSTP, it will look for WSMain, thus the linking error.

The solution is to choose one set of libraries and the corresponding tools and stick to them.

You might want to choose WSTP because that's the new thing and MathLink is considered deprecated.

Or you might choose MathLink because it's backwards compatible and also because LibraryLink still uses MathLink ... (although I haven't verified whether it breaks when trying to link against WSTP).

Contrary to what you may have heard, as of Mathematica 10.4 you lose absolutely nothing functionality-wise when choosing the 'deprecated' MathLink, but you do gain source-compatibility with older versions of Mathematica.

  • 1
    $\begingroup$ Ugh, this is ridiculous. Why would they make WTSP and MathLink incompatible with each other and require a separate toolchain for each? It might have been easier to rename everything and then define all the MathLink functions as aliases using headers for backward compatibility. At least this wouldn't require wholesale duplication. $\endgroup$ Commented May 11, 2016 at 13:28
  • $\begingroup$ @OleksandrR. I think that on some platforms MathLink programs link dynamically to the MathLink library. This means that in order for old (already compiled) programs to work with M10.0, Mathematica still need to ship with the ML prefix library. It also means that if we want to compile a MathLink program with M10.0 in such a way that it works with M9.0, it needs to be linked against an ML-prefix library. I think that the only reasonable solution would have been not to mess with an API for marketing reasons. Renaming MathLink is fine, but don't touch the API! Who cares what "ML" means anyway $\endgroup$
    – Szabolcs
    Commented May 11, 2016 at 13:54
  • $\begingroup$ @OleksandrR. Also, the transition must not have been smooth internally either because the MathKernel binary still has ML-prefix symbols inside. $\endgroup$
    – Szabolcs
    Commented May 11, 2016 at 13:56

I am not on Linux, but I'm pretty sure your problems stem from your not having WSTP/MathLink in your include or library search path.

What you probably should have done, rather than including the full path to wstp.h in your source file, is to add -I/usr/local/Wolfram/Mathematica/10.4/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/ to your link line. And you would also need to add -L/usr/local/Wolfram/Mathematica/10.4/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/ -lWSTP64i4 to take care of the unresolved dependency.

In sum, it should look like this:

mcc -Wall -I/usr/local/Wolfram/Mathematica/10.4/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/ -L/usr/local/Wolfram/Mathematica/10.4/SystemFiles/Links/WSTP/DeveloperKit/Linux-x86-64/CompilerAdditions/ -lWSTP64i4 -o f.exe f.tm f.c

Hopefully that should work, but as I said, I can't really test this because I'm not running Linux, so I could have missed something. More information is given in the documentation under WSTP Development in C (Linux)

By the way: for your actual problem, I'd be looking for a native C library for function minimization, if I were you, rather than trying to use Mathematica in that capacity. Of course, you can do if if you really need to, but it makes your life more difficult than necessary and currently you are going in the wrong direction (calling C from Mathematica, rather than calling Mathematica from C).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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