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.

razlebe
  • 7,134
  • 6
  • 42
  • 57
John Matok
  • 157
  • 1
  • 12
  • 1
    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? – Sjoerd C. de Vries Jun 30 '11 at 16:06
  • Yes, the demo file exists at this location: /Applications/Mathematica.app/AddOns/Applications/CCompilerDriver/\ SystemFiles/CSource/createDLL_demo.c – John Matok Jun 30 '11 at 17:36
  • @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) – acl Jun 30 '11 at 17:36
  • 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? – Brett Champion Jun 30 '11 at 18:32
  • 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. – John Matok Jun 30 '11 at 18:55
  • I'm now running OSX 10.6.7 and still no luck. – John Matok Jun 30 '11 at 21:01
  • No idea. I suggest contacting support@wolfram.com. – Brett Champion Jul 01 '11 at 03:57
  • I have same versions of Mac OS X, Mathematica, and `gcc`. I was able to run your code without any problem. – Meng Lu Jul 01 '11 at 05:19
  • See this http://reference.wolfram.com/mathematica/Compile/tutorial/CompilationTarget.html#130334773 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? – Szabolcs Jul 01 '11 at 12:17
  • @Szabolcs 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? – John Matok Jul 01 '11 at 14:17
  • @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 docs – Szabolcs Jul 01 '11 at 14:25
  • 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... – Daniel Chisholm Jul 01 '11 at 16:24

3 Answers3

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.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • 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? – John Matok Jul 01 '11 at 16:52
  • 1
    I just reinstalled Mathematica. Now I have the header files. I guess my original installation wasn't complete. Thank you for all your help. – John Matok Jul 01 '11 at 17:49
  • 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. – Leonid Shifrin Jul 02 '11 at 10:27
  • Thanks @Leonid, I made that point more explicit in the answer. – Szabolcs Jul 02 '11 at 13:43
  • @Szabolcs 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. http://stackoverflow.com/questions/26518086/mathematica-wont-compile-to-c-windows-8 – An old man in the sea. Oct 23 '14 at 10:48
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.

jfklein
  • 877
  • 1
  • 11
  • 13
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"]

Daniel Chisholm
  • 566
  • 5
  • 16
  • 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.2_ – John Matok Jul 01 '11 at 14:57
  • Perhaps your installation is incomplete, or damaged? On my machine $CCompilerDirectory has several ".m" files plus three directories ("Documentation", "Kernel", and "SystemFiles") – Daniel Chisholm Jul 01 '11 at 16:23