4

What options can be used for compilation using Compile or CreateExecutable in Mathematica and where can one find details on the possible values for the settings?

I have a recent build of MinGW installed on a Win7 workstation which works when called from the command line, but it isn't detected by Mathematica. The following should let Mathematica know where to find the compiler:

Needs["CCompilerDriver`GenericCCompiler`"]
$CCompiler = {"Compiler" -> GenericCCompiler, "CompilerInstallation" -> "C:\\Program Files\\mingw-builds\\x64-4.8.1-posix-seh-rev5\\mingw64\\bin","CompilerName" -> "x86_64-w64-mingw32-gcc.exe","CompileOptions" -> "-O2"};

Why does the following test code (from the specific compilers help) fail to compile? I assume Mathematica is still not recognizing the C compiler, but it's not clear why.

In[1]:= greeter = CreateExecutable[StringJoin[
   "#include<stdio.h>\n",
   "int main(){\n",
   " printf(\"Hello MinGW-w64 world.\\n\");\n",
   "}\n"],
  "hiworld", "Compiler" -> GenericCCompiler,
  "CompilerInstallation" -> 
   "C:\\Program Files\\mingw-builds\\x64-4.8.1-posix-seh-rev5\\mingw64\\bin", 
  "CompilerName" -> "x86_64-w64-mingw32-gcc.exe"]
Out[1]= $Failed

Setting the "Debug"->True for CreateExecutable gives us some additional output:

In[2]:= greeter = (*same as above, with "Debug"->True*)

C:\Users\myusername\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-mycomputer-1000-1000-1>C:\Program Files\mingw-builds\x64-4.8.1-posix-seh-rev5\mingw64\bin\x86_64-w64-mingw32-gcc.exe -o

"C:\Users\myusername\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-mycomputer-1000-1000-1\hiworld.exe"

-g -I"C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\IncludeFiles\C" -I"C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\MathLink\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\include"

"C:\Users\myusername\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\Working-mycomputer-1000-1000-1\hiworld.c"

-L"C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\MathLink\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\lib"

-L"C:\Program Files\Wolfram Research\Mathematica\10 .0\SystemFiles\Libraries\Windows-x86-64" 2>&1

C:\Program is not recognized as an internal or external command, operable program or batch file.

Out[2]= $Failed

The -L (libraries path) and -I (include path) options shown in the output are mentioned briefly in the gcc docs and in this answer by @Szabolcs.

dionys
  • 4,321
  • 1
  • 19
  • 46
  • Does $CCompiler = {"Compiler" -> CCompilerDriver\MinGWCompiler`MinGWCompiler, "CompilerInstallation" -> "C:\Program Files\mingw-builds\x64-4.8.1-posix-seh-rev5\mingw64\bin", "CompilerName" -> Automatic};` help? – xzczd Oct 09 '14 at 07:15
  • @xzczd Do you have MinGWCompiler defined on your system? When I try with that setting I get the CreateExecutable::badcomp error message, which states: Compiler specification "Compiler"->CCompilerDriver\MinGWCompiler`MinGWCompiler does not specify a compiler driver listed by CCompilers[Full].` – dionys Oct 09 '14 at 08:21
  • I have TDM-GCC installed in my computer. What's the output if you run CCompilers[Full]? – xzczd Oct 09 '14 at 08:25
  • 1
    @xzczd With a fresh kernel after calling Needs["CCompilerDriver\"];Needs["CCompilerDriver`GenericCCompiler`];,CCompilers[Full]returns{{Name->Intel Compiler,Compiler->CCompilerDriver`IntelCompiler`IntelCompiler,CompilerInstallation->None,CompilerName->Automatic},{Name->Generic C Compiler,Compiler->GenericCCompiler,CompilerInstallation->None,CompilerName->Automatic}}` – dionys Oct 09 '14 at 08:44
  • Try : "CompilerInstallation" -> "C:\Program Files\mingw-builds\x64-4.8.1-posix-seh-rev5\mingw64" – Rolf Mertig Oct 09 '14 at 09:26
  • @RolfMertig I guess the \\bin isn't the issue. Just found a 64-bit machine and tested OP's settings, I only changed the "CompilerInstall‌​ation" to mine i.e. "C:\\TDM-GCC-64\\bin", and it works well. BTW, why you think your test code failed to compile? What's the output of your greeter? – xzczd Oct 09 '14 at 10:22
  • @xzczd and @RolfMertig, I edited the the question to include the output of greeter. – dionys Oct 09 '14 at 11:51
  • 1
    "C:\Program is not recognized……" seems that the white space in the path isn't recognized correctly. I'm not sure about the reason. Did you forget to add double quotation marks somewhere? – xzczd Oct 10 '14 at 04:08
  • @xzczd Doesn't seem to help. Adding quotes to "CompilerInstallation" setting results in $Failed and gives the error CreateExecutable::instl, and there aren't any spaces or strange characters in the "CompilerName" setting. What else can be quoted or escaped? – dionys Oct 10 '14 at 12:59
  • Can it be an issue of compiler installation? Maybe quotation isn't used in some places when the compiler is installed, and it doesn't cause any problem when called from command line but does when called from Mathematica? – xzczd Oct 10 '14 at 13:07
  • Does it work if you prefix the space in "Program Files" with a single backslash? Or maybe use "CompilerInstallation" -> "\"C:...bin\""? – celtschk Oct 18 '14 at 22:40
  • @celtschk I'm afraid not. I've tried escaping spaces and adding escaped quotes. Just tested again to double check and no dice. – dionys Oct 18 '14 at 22:45
  • Did you try them separately? – celtschk Oct 18 '14 at 22:52
  • If I purposely specify junk for the installation directory I get the error: CreateExecutable::instl. So I think the installation directory is being read properly either way (spaces escaped and no quotes, or everything in escaped quotes). But I'm still getting $Failed from CreatExecutable and I get the error: Compile::nogen from Compile. – dionys Oct 18 '14 at 23:09
  • Have you considered reinstalling the compiler in a directory without white spaces? – xzczd Oct 20 '14 at 05:31

0 Answers0