Message Boards Message Boards

Find gcc compiler using Mathematica V12?

I installed gcc but Mathematica (12) cannot seem to find it.

I installed gcc two ways: (i) via MinGW and (ii) CodeBlocks. A windows command line check confirms the compilers are installed.

I updated my Windows Environments. GetEnvironment[] now shows Path -> ..... C:\ Program Files (x86)\ CodeBlocks \ MinGW \ bin;C:\ MinGW \ \ ....

Still Mathematica does not find it. Executing

Needs["CCompilerDriver`"]
CCompilers[]
CCompilers[Full]

yields

{}
{{"Name" -> "Intel Compiler", 
  "Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler, 
  "CompilerInstallation" -> None, 
  "CompilerName" -> Automatic}, {"Name" -> "Generic C Compiler", 
  "Compiler" -> CCompilerDriver`GenericCCompiler`GenericCCompiler, 
  "CompilerInstallation" -> None, "CompilerName" -> Automatic}}

Attempting to Compile using CompilationTarget -> "C" produces the errors

- CreateLibrary::nocomp: A C compiler cannot be found on your system. Please consult the documentation to learn how to set up suitable compilers.
- Compile::nogen: A library could not be generated from the compiled function.

I consulted the documentation, but don't get anywhere.

PS: tried compiling using the new FunctionCompile but this produces much slower code than Compile without CompilationTarget -> "C"

Thanks, Eric

POSTED BY: Eric Michielssen
16 Replies

wstp.h ( from WSTP ) use MSVC specific __int64 type.

I want to use pari and gap functions in Mathematica through WSTP.

But parilib and libgap require the gcc compiler.

Any suggestions?

POSTED BY: nilo de Roock
Posted 4 years ago

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.

POSTED BY: Joel Klein

MathOptimizer Professional is a Mathematica interface to the standalone optimization code LGO and therefore does not use CCompilerDriver. It uses Format.m to convert the Mathematica objective functions and constraints to Fortran or C and then uses a DOS call to call the compiler.

POSTED BY: Frank Kampas

Frank,

All that said, of course there can be valid reasons to want to use gcc with Mathematica on Windows. Since you seem to be doing this, can you give us an updated set of instructions on how to set this up, in particular for C++ code (not just plain C)?

The CCompilerDriver user guide does have instructions for MinGW-w64. I think they are outdated, but I have not tried recently. http://reference.wolfram.com/language/CCompilerDriver/tutorial/Overview.html

There are several comments on SE as well, again most outdated, and most incomplete. https://mathematica.stackexchange.com/search?q=mingw and https://mathematica.stackexchange.com/q/55034/12

I have gotten it to work in the past, but have not tried recently. As I remember, it was not straightforward. In particular, if one needs MathLink/WSTP, as I remember, the libraries needed to be converted to a compatible format. CompilationTarget -> "C" does not need this, but more complex LibraryLink code does.

http://mathforum.org/kb/message.jspa?messageID=7841068

POSTED BY: Szabolcs Horvát

Can you tell me why Wolfram Research doesn't support a compiler as widely used as gcc ?

POSTED BY: Frank Kampas

Is gcc widely used on Windows?

Mathematica itself is compiled with MSVC, and the Windows version is not 7-8 times slower than the Linux version when run on the same computer.

It makes sense to use the operating system vendor's own toolchain.

There isn't even a common standard for gcc on Windows. There are multiple incompatible versions out there. The code generated by different ones can't be linked safely.

POSTED BY: Szabolcs Horvát

MathOptimizer Professional

https://www.wolfram.com/products/applications/mathoptpro/

uses a C or Fortran compiler to evaluate the objective function and constraints. It runs 7 or 8 times faster with GCC as the C compiler as compared to using Visual Studio as the C compiler.

POSTED BY: Frank Kampas

This is still anecdotal (the package is not free to try for all), and does not match my own experience at all. I did compare VS and gcc on several performance-critical programs I wrote.

Can you show a short standalone C program that runs 7-8 times faster when compiled with VS than with gcc?

POSTED BY: Szabolcs Horvát

In that case, I'd be very interested in getting it to work. Any idea where I may be going wrong? (see my first post.) Thanks.

POSTED BY: Eric Michielssen

I've used both Visual Studio and gcc. gcc is 7 or 8 times faster.

POSTED BY: Frank Kampas

I've used both Visual Studio and gcc. gcc is 7 or 8 times faster.

This is not generally the case. No production-quality C++ compiler produces 7 times faster code than another one. When such things happen, the explanation is usually not the compiler.

I contest the idea that gcc is even a little bit faster than VS. When I worked on Windows, I encountered both situations: when gcc produced slightly faster code and when VS produced slightly faster code.

If you can show a concrete example where gcc appears to produce 6-7 times faster code, I am sure we can track down the reason and show it not to be truly compiler-related (and not to be relevant for most applications). I would find it very very strange to see such an example at all.

POSTED BY: Szabolcs Horvát

Do you need gcc specifically? Mathematica supports the Visual Studio compiler very well, and can detect it without any configuration. Google for "visual studio build tools" to get just the compiler without the IDE (if you don't want the IDE).

I would recommend using VS unless you have a good reason to use gcc. If all you need is CompilationTarget -> "C", then VS is the better choice.

POSTED BY: Szabolcs Horvát

Thanks -- that worked!

POSTED BY: Eric Michielssen
Posted 4 years ago

Thanks

POSTED BY: Haobo Xia
Posted 4 years ago

Szabolcs,

I'm a bit confused on which version of VS one needs and what packages/extras are required. I've donwloaded VS 2015 build tools for Win7, but Mathematica still does not recognize a C compiler. I could download VS 2019 build tools, but according to the installer it is huge (4+ GB). This seems to be an absolute overkill just to have a C compiler. In the meantime, I've managed to make Mathematica recognize MinGW gcc (alltogether 440MB). Do you have any idea on how to install a minimal VS C compiler that is considerably less than 4GB in overall size and that is immediately recognized by Mathematica?

POSTED BY: Updating Name

Unfortunately, I do not know the answer to this. I rarely use Windows these days, and when I do, I usually install the Visual Studio command line tools, even if they are large.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract