Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC installed. Mathematica still won't compile to C

I'm running Mathematica 8 on a MacOSX, trying to compile even the simplest program to C. Anything having to do with C simply doesn't work in Mathematica. I have GCC 4.2 installed; I've even reinstalled it multiple times with XCode. Here's what I'm doing and the errors I'm getting:

First, I always evaluate the command

Needs["CCompilerDriver`"]

If I set the compilation target to C,

c = Compile[ {{x}}, x^2 + Sin[x^2], CompilationTarget -> "C"];

I get an error that reads: Compile::nogen : A library could not be created from the compiled function.

If I try to create a library,

demoFile = FileNameJoin[{$CCompilerDirectory,"SystemFiles","CSource","createDLL_demo.c"}];
lib = CreateLibrary[{demoFile},"testLibrary"]

I get an message $Failed. Wolfram says that this is because I don't have a C compiler installed. I find that hard to believe because when I run

CCompilers[]

It tells me that I've got GCC installed: {{"Name" -> "GCC", "Compiler" -> CCompilerDriver'GCCCompiler`GCCCompiler, "CompilerInstallation" -> "/usr/bin", "CompilerName" -> Automatic}}

What's more, terminal says I have GCC installed too!! Any help would be appreciated. I'd really like to compile Mathematica to C.

like image 895
John Matok Avatar asked Jun 30 '11 15:06

John Matok


2 Answers

In this answer I'll collect some debugging steps for similar problems, for future reference. Feel free to edit/improve them.

If compiling to C code does not work from Mathematica 8,

  1. Check that you have a supported C compiler installed and it works (the obvious).

    Note that the compiler does not necessarily have to be in the PATH, at least on Windows/Visual Studio it doesn't.

  2. Check that Mathematica recognizes the compiler

    << CCompilerDriver`
    CCompilers[]
    

    will list the compilers known to Mathematica.

  3. Check what commands Mathematica executes to compile the generated C code:

    Compiler`$CCompilerOptions = {"ShellCommandFunction" -> Print};
    Compile[{{x}}, x^2, CompilationTarget -> "C"];
    

    Note that with "ShellCommandFunction" -> Print the commands will not be executed, so you'll need to re-set Compiler`$CCompilerOptions to {} after this step is complete to allow command execution again.

  4. Check the output/errors from the compiler:

    Compiler`$CCompilerOptions = {"ShellOutputFunction" -> Print};
    Compile[{{x}}, x^2, CompilationTarget -> "C"];
    

These last two steps will hopefully give you enough clues to proceed. With this information you can check if the correct library / include paths are passed to the compiler (in the case of gcc/icc, look at the -L option which specifies library paths and the -I option which specifies include paths). Then check if the required include and library files are present at those paths.

like image 160
Szabolcs Avatar answered Oct 13 '22 22:10

Szabolcs


If you get Compile::nogen, you can see the compiler output by setting ShellOutputFunction->Print right in the Compile expression:

c = Compile[ {{x}}, x^2 + Sin[x^2], 
   CompilationTarget -> {"C", "ShellOutputFunction"->Print}];

In general, this is how you can pass options to the underlying CreateLibrary call, by changing CompilationTarget->"C" to CompilationTarget->{"C", options}. Setting Compiler`$CCompilerOptions works too, but this technique has the advantage of not setting a global variable.

like image 22
jfklein Avatar answered Oct 13 '22 23:10

jfklein