Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to set up my new Windows 8.1 tablet to compile Mathematica (9.0.1) functions to C, using the MinGW-64 compiler. I have it set up so that the following works:

greeter = CreateExecutable[StringJoin[
    "#include <stdio.h>\n",
    "int main(){\n",
    "  printf(\"Hello MinGW-w64 world.\\n\");\n",
Import["!\"" <> greeter <> "\"", "Text"]

However, function compilation to C fails, as follows:

zeroC = Compile[{
   CompilationTarget -> "C"

CreateLibrary::cmperr: "Compile error: collect2.exe: error: ld returned 1 exit status

Compile::nogen: A library could not be generated from the compiled function.

On an older computer that I had previously set up for compilation, this same function definition works fine.

Any clues as to what is going wrong? It would of course be helpful if I could see exactly what commands Mathematica is passing to the shell, and what complaint ld makes. CreateExecutable has an option "ShellOutputFunction" -> Print that shows what it's doing, but I can't figure out how to pass this to Compile.

share|improve this question
How to set up MinGW-64 is a good question. But if you want to avoid trouble and have reliably working compilation with minimal fuss, then just install Microsoft's compiler. I recommend this version, which is not the latest, but will only install command line tools and takes a bit less disk space. It works out of the box with Mma. See here. – Szabolcs Jul 16 '14 at 16:39
I tried that: it wouldn't install. I assumed without investigating that it was an incompatibility between Windows 8.1 and the Win7 SDK. – Leon Avery Jul 16 '14 at 17:02
I have successfully installed it on Windows 8.1, so it's not incompatible. (Which I realize doesn't help you install it, but it's good to note that the problem was not incompatibility.) – Szabolcs Jul 16 '14 at 17:07
Hmm, thanks. If I hadn't solved the problem with MinGW, your remark would have spurred me to try harder to get Visual Studio working. As it is, I think I'll let it ride. I've been using MinGW for a couple years now and it works fine for me. – Leon Avery Jul 16 '14 at 17:18
up vote 10 down vote accepted

Figured it out. The key was working out that if I included "ShellOutputFunction"->Print in the $CCompiler list, compilation of the function would show me what the shell was doing. Thus I learned that it was failing to find libgcc_s. I found libgcc_s.a in C:\MinGW-64\lib. I created a system environment variable LIBRARY_PATH with value C:\MinGW-64\lib, and the function now compiles and works. (It also works to add "CompileOptions" -> {"-O2", "-LC:/MinGW-64/lib"} to $CCompiler.)

Edit: Here's a summary of what it takes to get MinGW-64 working with Mathematica's Compile and LibraryLink:

  1. Download the package manager from At the time I grabbed it, it was yypkg-1.4.0.exe. Double-click on it and install the 64-bit compiler to C:\MinGW-64.

  2. In the System control panel (well-hidden in Windows 8.1, but you can find it by hitting the Windows key + X), click Advanced settings, then Environment variables. Add ;C:\MinGW-64\bin to the end of Path. Create a new environment variable called LIBRARY_PATH and set its value to C:\MinGW-64\lib.

  3. Add the following to your init.m:

    $CCompiler={"Compiler"->GenericCCompiler, "CompilerInstallation"->"C:/MinGW-64","CompilerName"->"x86_64-w64-mingw32-gcc.exe", "CompileOptions"->"-O2"};

    I put this in my user init.m in C:\Users\USERNAME\AppData\Roaming\Mathematica\Kernel\. If more than one user uses the machine, you might want to put it in the system-level init.m instead.

    -O2 is the usual recommended optimization level. You don't need this, but it can speed things up a lot, which is presumably why you're compiling functions to C in the first place. Try -O3 if you like living dangerously.

  4. Try it out with the definitions I posted in the question.

share|improve this answer
Related: It looks like to use MathLink (not LibraryLink) some libraries need to be converted first. I don't know much about this. – Szabolcs Jul 17 '14 at 14:34
-O3 in gcc is supposed to maintain standards compliance, unlike in e.g. Intel C. -Ofast would be the potentially dangerous option. N.B. -O3 does not always produce faster code than -O2 and can actually be slower due to producing more, or more nonlocal memory accesses, or expanding the working set. – Oleksandr R. Jul 17 '14 at 15:27

Your Answer


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.