Short version: What's the simplest way to use MacPorts' gcc with CreateLibrary?

Long version:

CreateLibrary uses /usr/bin/gcc by default on OS X. I can get it to use clang by using the option "CompilerName" -> "clang++" or "CompilerName" -> "c++" (I'm using C++).

However, I would like to use MacPorts' gcc because it boosts the performance significantly for this particular program I need to compile. It also supports OpenMP; the system compiler doesn't. "CompilerName" -> "g++-mp-5" tells me that

CreateLibrary::instl: The compiler installation directive "CompilerInstallation" -> "/usr/bin" does not indicate a usable installation of "GCC"

So I tried "CompilerInstallation" -> "/opt/local/bin", which is the location of g++-mp-5. This results in the same error:

CreateLibrary::instl: The compiler installation directive "CompilerInstallation" -> "/opt/local/bin" does not indicate a usable installation of "GCC"

Is there a simpler solution than using GenericCCompiler?

  • $\begingroup$ Check the source code for the CCompilerDriver` package. You might be surprised at how simplistic the checks are for whether a compiler is "usable" or not. No doubt one could arrive at some fairly simple modifications to the package to make it work properly. $\endgroup$ Sep 5, 2013 at 21:04
  • $\begingroup$ I get the impression that a driver has to be created to fully use a particular compiler. As already suggested, look at the other drivers in that directory. Now the next question is, once created, how does one integrate the driver into the system? $\endgroup$
    – rcollyer
    Sep 5, 2013 at 21:09
  • $\begingroup$ @rcollyer a "driver" just provides the right command line arguments and link libraries. Writing a new one does not seem to be that difficult, although strictly speaking since CCompilerDriver` package is not "supplied as an example" (or so one would suppose), modifying it would contravene the Mathematica licence. $\endgroup$ Sep 5, 2013 at 21:53
  • $\begingroup$ @OleksandrR. yes, and more specifically, it tells CCompilerDriver` how to interface with the compiler. So, while there isn't an "example" of how to do it, you could learn how by looking at the packages supplied. $\endgroup$
    – rcollyer
    Sep 5, 2013 at 23:00
  • $\begingroup$ Here's a guess: set "Compiler" -> CCompilerDriver`GCCCompiler`GCCCompiler, in addition to "CompilerInstallation" and "CompilerName". $\endgroup$
    – rcollyer
    Sep 6, 2013 at 3:09

1 Answer 1


I ended up using the GenericCCompiler driver, as follows:

$CCompiler = {
 "Compiler" -> CCompilerDriver`GenericCCompiler`GenericCCompiler, 
 "CompilerInstallation" -> "/opt/local/bin", 
 "CompilerName" -> "g++-mp-5", 
 "SystemCompileOptions" -> "-O2 -m64 -fPIC -framework Foundation -framework mathlink"

The compiler definition should use "SystemCompileOptions" instead of "CompileOptions" so that the user can set their own custom "CompileOptions" when desired.

CreateLibrary[{"mylib.cpp"}, "mylib", 
 "IncludeDirectories" -> "/opt/local/include", 
 "ShellOutputFunction" -> Print, "ShellCommandFunction" -> Print, 
 "Language" -> "C++"]

It is good to note that the "Libraries" and "SystemLibraries" options are not usable on OS X because this driver auto-appends .lib to any library name, which is incorrect on OS X.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.