GCC installed. Mathematica still won't compile to C
Asked Answered
C

3

11

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.

Corfu answered 30/6, 2011 at 15:45 Comment(12)
The documentation says MMA has been tested with GCC 4.0. However, I wouldn't expect a minor version difference to be the source of your problems. Did you check whether the demo file exists?Annam
Yes, the demo file exists at this location: /Applications/Mathematica.app/AddOns/Applications/CCompilerDriver/\ SystemFiles/CSource/createDLL_demo.cCorfu
@sjoerd it's not the gcc version, eg in mine, Import["!gcc --version", "Text"] says i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3) (followed by the copyright notification)Twopiece
What version of Mathematica, and what version of OS X? It works for me with V8.0.1 and OS X 10.6.8. (My version of GCC is the same as acl's.) I assume you can successfully compile a simple "hello world" program?Embrocate
I just compiled and ran a "hello world" program using gcc in the terminal. It worked fine. I'm running OSX 10.6.7 and V8.0.0.Corfu
I'm now running OSX 10.6.7 and still no luck.Corfu
No idea. I suggest contacting [email protected].Embrocate
I have same versions of Mac OS X, Mathematica, and gcc. I was able to run your code without any problem.Wilburn
See this reference.wolfram.com/mathematica/Compile/tutorial/… and the following section in the docs. This should give more info about what's happening on your machine when the compiler is being called. What output do you get?Estellestella
@Estellestella I get a good number of errors. The first two say that there's no WolframLibrary.h and WolframCompileLibrary.h files. The errors following that are associated with missing operators. Could my problem be that I'm missing these header files?Corfu
@Nick, then look at the commands that are executed, and check that the correct include paths (-I) are passed to the compiler. Use Compiler`$CCompilerOptions = {"ShellCommandFunction" -> Print}; as per the docsEstellestella
Not finding "WolframLibrary.h" sounds suspicious; FWIW on my machine it is located at /usr/local/Wolfram/Mathematica//8.0/SystemFiles/IncludeFiles/C/WolframLibrary.h if that is of any help...Mincey
E
13

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.

Estellestella answered 1/7, 2011 at 14:46 Comment(5)
Bear with me; this is becoming unfamiliar to me. I ran the ShellCommandFunction and it gave me two -I and two -L paths. I don't know what they should be, so I don't know if they're wrong. The only thing that stood out to me is that the first path, -I"/Applications/Mathematica.app/SystemFiles/IncludeFiles/C" didn't contain any files. My hunch is that there should be something here. I mentioned before, but I'll write it again. ShellOutputFunction tells me that there's no WolframLibrary.h and WolframCompileLibrary.h headers. At this point, should I just reinstall Mathematica?Corfu
I just reinstalled Mathematica. Now I have the header files. I guess my original installation wasn't complete. Thank you for all your help.Corfu
I'd guess that "I" path is the path for the compiler to search for include files (.h) and "L" path is the library path, for the linker to search for additional libraries you are linking your code with.Pitchford
Thanks @Leonid, I made that point more explicit in the answer.Estellestella
@Estellestella Would you mind taking a look at my question? Is very similar, but on Windows 8. And I'm not sure how to use the info of points 3 and 4...Any help would be appreciated. #26518586Honea
C
3

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.

Carcassonne answered 14/10, 2011 at 19:4 Comment(0)
M
1

It is a shame that the only error you are seeing is $Failed, that's not terribly helpful; I wonder if perhaps there are some file or directory permissions problems?

I'm running on linux not Mac so I am not sure if my setup is "close enough" or not. On my machine your Compile command succeeds and generates a file .Mathematica/ApplicationData/CCompilerDriver/BuildFolder/blackie-desktop-5077/compiledFunction1.so in my home directory. Is there any way you can find a .Mathematica directory associated with your userid, and see if it exists and is writeable by mathematica?

Also, you could check to see if "gcc" is or is not being accessed by checking the file access time of /usr/bin/gcc before and after your call to Compile. From an operating system shell you can do ls -lu /usr/bin/gcc or from Mathematica perhaps Import["!ls -lu /usr/bin/gcc", "Text"]

Mincey answered 1/7, 2011 at 13:2 Comment(2)
I found a directory /Users/Noon/Library/Mathematica/ApplicationData/CCompilerDriver/BuildFolder/goble-1586 and it's writable by everyone. There seems to be no files in the CCompilerDirectory. Should there be? I just mentioned above that Mathematica can't find WolframLibrary.h and WolframCompileLibrary.h header files. Could that be the issue? GCC is being accessed by Mathematica. /usr/bin/gcc -> gcc-4.2Corfu
Perhaps your installation is incomplete, or damaged? On my machine $CCompilerDirectory has several ".m" files plus three directories ("Documentation", "Kernel", and "SystemFiles")Mincey

© 2022 - 2024 — McMap. All rights reserved.