Can you tell me why Wolfram Research doesn't support a compiler as widely used as gcc ?
Original developer of CCompilerDriver here. Szabolcs is right (as usual), it's not a question of gcc, which we've supported on Unix from the beginning, but MinGW gcc on Windows 64-bit, which boils down to why doesn't MinGW support gcc on 64-bit, though there is a 64 bit port that you can use, and using that is documented.
What you're reporting is the automatic compiler finding behavior, and also running into the fact that MinGW's C compiler driver is only loaded on 32-bit Windows, for a good reason. At the time this was added, MinGW did not produce 64 bit binaries, and that is required to load the binaries into the Mathematica kernel on a 64 bit OS, which is what happens with CompilationTarget -> "C"
option of Compile
, or the CreateLibrary
function in general.
I've passed this thread on the CCompilerDriver maintainer, to see if anything can be done. But in the interest of helping, here's what I see.
It looks to me like MinGW's gcc still only produces 32 bit binaries. I downloaded MinGW onto my 64-bit Windows 10 machine and compiled hello, world, and it looks to still be 32 bit:
$ file hello
hello: PE32 executable (console) Intel 80386, for MS Windows
You can ignore this and try loading the driver's package:
Needs["CCompilerDriver`MinGWCompiler`"]
For me, after this point CCompilers[]
is detecting the compiler. However, it does not work (as expected):
In[6]:= CCompilers[]
Out[6]= {{"Name" -> "Visual Studio", "Compiler" ->
CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler,
"CompilerInstallation" -> "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools",
"CompilerName" -> Automatic}, {"Name" -> "MinGW", "Compiler" -> MinGWCompiler,
"CompilerInstallation" -> "C:\\MinGW\\bin\\gcc.exe", "CompilerName" -> Automatic}}
In[16]:= $CCompiler = Last[CCompilers[]]
Out[16]= {"Name" -> "MinGW", "Compiler" -> MinGWCompiler, "CompilerInstallation" -> "C:\\MinGW\\bin\\gcc.exe", "CompilerName" -> Automatic}
In[17]:= cf = Compile[{{x, _Real}}, Sin[x] + x^2 - 1/(1 + x), CompilationTarget -> "C"]
During evaluation of In[17]:= Compile::nogen: A library could not be generated from the compiled function.
Out[17]= CompiledFunction[{11, 12.1, 5468}, {
Blank[Real]}, {{3, 0, 0}, {3, 0, 1}}, {{1, {2, 0, 0}}}, {0, 1, 5, 0, 0}, {{40, 1, 3, 0, 0, 3, 0, 1}, {40,
56, 3, 0, 0, 3, 0, 2}, {10, 0, 3}, {13, 3, 0, 3}, {40, 60, 3, 0, 3, 3, 0, 4}, {19, 4, 3}, {13, 1, 2, 3, 1}, {1}},
Function[{x}, Sin[x] + x^2 - 1/(1 + x)], Evaluate]
I think, the if you want MinGW/gcc, you need to use the MinGW-w64 project.
The documentation talks about how to use this, look for the section "MinGW for 64-bit targets":
https://reference.wolfram.com/language/CCompilerDriver/tutorial/SpecificCompilers.html
This also shows how to use the "GenericCCompiler" driver, which you can use for any C compiler. It just didn't get found automatically but you can still use it by telling the system where it lives.