MinGW-64 and CompilationTarget -> "C": can't make library
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:
Download the package manager from win-builds.org. At the time I grabbed it, it was
yypkg-1.4.0.exe
. Double-click on it and install the 64-bit compiler toC:\MinGW-64
.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 ofPath
. Create a new environment variable calledLIBRARY_PATH
and set its value toC:\MinGW-64\lib
.Add the following to your
init.m
:Needs["CCompilerDriver`"] Needs["CCompilerDriver`GenericCCompiler`"] $CCompiler={"Compiler"->GenericCCompiler, "CompilerInstallation"->"C:/MinGW-64","CompilerName"->"x86_64-w64-mingw32-gcc.exe", "CompileOptions"->"-O2"};
I put this in my user
init.m
inC:\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.Try it out with the definitions I posted in the question.
This is both a comment, too long to be posted as a comment, on the previous answer, and another answer. The reason for posting is that the steps that I had to do to get MinGW-64 working on my computer are a little bit different and simpler than the steps given by Leon.
Obviously, the first step is to get MinGW-64. I was unable to do this on win-builds.org, but just as in this question, I downloaded from sourceforge.net the executable mingw-w64-install.exe. I ran it with the settings Architecture: x86-64 and Threads: win32. That took about 15 minutes. The progress bar was much faster at the end than at the beginning. Inside the map mingw-w64, two levels down, there is a map mingw64. I copied this map to C:\ and renamed it as MinGW-64. Now we are the situation described in the tutorial "Specific Compilers" under the head "MinGW for 64-Bit Targets". So I tried the greeter test mentioned there:
greeter=CreateExecutable[StringJoin[
"#include <stdio.h>\n",
"int main(){\n",
" printf(\"Hello MinGW-w64 world.\\n\");\n",
"}\n"],
"hiworld","Compiler"->GenericCCompiler,"CompilerInstallation"->"C:\\MinGW-64\\bin","CompilerName"->"x86_64-w64-mingw32-gcc.exe"]
(*C:\Users\Fred Simons\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\hiworld.exe*)
It worked immediately. (Observe that there are no spaces in the path to MinGW-64, and that we may leave out '\bin'. See also my comment at the end of this answer.)
Import["!\""<>greeter<>"\"","Text"]
(* Hello MinGW-w64 world. *)
This works as well. Next, as in the tutorial, I defined $CCompiler:
$CCompiler = {"Compiler"->GenericCCompiler, "CompilerInstallation"->"C:\\MinGW-64\\bin","CompilerName"->"x86_64-w64-mingw32-gcc.exe"};
f = Compile[{x}, Sqrt[1+x^2], CompilationTarget->"C"];
f[1]
(* 1.41421 *)
So everything works fine. Even better, it behaves like a correct installation of a C-compiler, without having placed anything in an init.m file or having created system variables; in every session of Mathematica it works immediately.
A side remark. Actually, I described my installation of MinGW in Mathematica 10. When I started the testing of Mathematica 11, it did not work any more. I had a very good support of WRI. The reason was that in the map \SystemFiles\FrontEnd\Binaries\Windows-x86-64 in the installation directory there was a file libwinpthread-1.dll, which is missing in the map for Mathematica 11. Copying this file from Mathematica 10 to Mathematica 11 solved the problem. In Mathematica 11.0.0 this file seems to be not needed anymore.
After all, installing the MinGW compiler on my computer turned out to be simple. It takes 'only' 421 MB of memory, while an installation of Windows Visual Studio takes at least about 2GB. I hate my computer to be polluted with a lot of software that I do not need. But my installation seems to have a problem: it is not recognized by Mathematica as an installed compiler. CCompilers[]
returns an empty list and SystemInformation[]
tells me that there are no C-compilers installed, despite the fact that I have it working. I have no idea if this is a serious problem. It works flawless for already a year, both in Mathematica 10 and in Mathematica 11.