10
$\begingroup$

I need to enable C++11 features when using CreateLibrary. I need a solution which will work with (almost) any compiler on any operating system.

Generally, this can be done by passing a certain flag to the compiler. In principle, the syntax will depend on the specific compiler. Clang and GCC take -std=c++11 while MSVC needs no special flag. The Intel compiler needs different flags depending on operating system (according to the documentation—I haven't had the chance to try it out). I am not sure what other compilers Mathematica may support.

The challenge is to select the correct compiler option to use. Thus the main part of my question is:

Given access to all CreateLibrary options (such as "Compiler", etc.), how can I robustly figure out which compiler it is going to use, so I can append the correct compiler flag?

Note: For my use case it can be assumed that we are compiling C++ code, not C code.

$\endgroup$
4
$\begingroup$

If the "Compiler" option is Automatic, then the default C compiler is used. This can be retrieved using

DefaultCCompiler[]

This function does not have its own documentation page, but it is mentioned here and it has a usage message.

Then we need to know all the possible symbols for compilers, and have the proper option for each. Here's a list:

CCompilerDriver`ClangCompiler`ClangCompiler
CCompilerDriver`GCCCompiler`GCCCompiler

CCompilerDriver`IntelCompiler`IntelCompiler

CCompilerDriver`CygwinGCC`CygwinGCC
CCompilerDriver`MinGWCompiler`MinGWCompiler
CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler

CCompilerDriver`GenericCCompiler`GenericCCompiler

The list is also found here.

Of these, Clang and GCC use the option style -std=c++11. CygwinGCC and MinGWCompiler are also GCC.

GenericCCompiler seems to default to gcc-like options, so using the same option style also seems reasonable. Alternatively, it may be left up to the user to set this, as this compiler can in principle be anything and does require some setup.

Visual Studio does not require any special option.

The Intel compiler behaves differently on different operating systems, according to its documentation. On Windows it requires /Qstd=c++11 and on Linux/OSX -std=c++11.


This option can then be added to "CompileOptions" or "SystemCompileOptions". If it is added to the latter, then the original "SystemCompileOptions" should also be preserved. These are in Options[drivername], not in Options[CreateLibrary].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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