Specific Compilers
Choosing the Default Compiler | Intel Compiler |
Setting a Different Compiler | Cygwin |
GCC | Generic C Compiler |
Visual Studio | No Compiler |
MinGW |
The CCompilerDriver package lets you work with C compilers that are installed on your computer. It lets you build executables, libraries, and object files from C source code. It is called automatically by the Wolfram System compiler when you set the option CompilationTarget to "C". It is also useful for building WSTP executables as well as Wolfram Libraries (dynamic link libraries that can be linked into the Wolfram Language).
This section describes how the CCompilerDriver package works with different compilers. It discusses how the package chooses which compiler to use and how you can change the compiler. In addition, it will give you an idea about how you can support a compiler that is not listed here.
Choosing the Default Compiler
When the CCompilerDriver package is initialized, it tries to find compilers that are supported and that it can find on your machine. Typically, compilers can be found by environment variables or on the path. Environment variables are set up by some compilers that have an installer—for example, the Visual Studio sets up an environment variable that indicates its location.
To use the package, it must first be loaded.
Now you can see the supported compilers that are found on your machine.
Note that CCompilers[] shows only the compilers that are actually found. However, you can see all the compilers that can be used on this type of machine.
In these results, the only compiler for which an actual installation could be found was Visual Studio. Other compilers, such as the Intel compiler, could not be found. This does not necessarily mean that the Intel compiler is not present, it just means that it could not be found.
The first compiler that is actually found is set as the default.
The default compiler can be changed by assigning to $CCompiler.
Setting a Different Compiler
If you want to work with a compiler that is not set up to be the default, you can do this by using some of the options for setting the compiler.
The option "Compiler" is used to set the type of compiler, such as Visual Studio or GCC. This might be useful if you have more than one compiler installed.
The option "CompilerInstallation" is used to set the location of the compiler software. Some compilers use environment variables to advertise their location and some are automatically found on a path. However, if your compiler cannot be found by one of these mechanisms you can use this option.
The option "CompilerName" is used to set the name of the compiler executable. This is useful if the compiler software supports more than one compiler command; for example, GCC can also be known as g++.
To use these settings globally for all uses of the CCompilerDriver package, you could set them with $CCompiler, as shown in the following.
$CCompiler =
{"Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler,
"CompilerInstallation" -> "E:\\IntelCompiler"}
Instead, if you wanted to use the setting just for one invocation of a compiler function, you could set them as options. This is demonstrated below.
CreateLibrary[ {"sourceFile.c"}, "myLib",
"Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler,
"CompilerInstallation" -> "E:\\IntelCompiler"]
GCC
The CCompilerDriver package has been tested with several versions of the GNU Compiler Collection (http://gcc.gnu.org) on a variety of different platforms.
Tested versions of GCC and platform.
The default "SystemCompileOptions" setting is "-O2", which gives optimized code without generating large files. If you wanted to debug your code in a C language debugger, you could set the option "Debug" to be True. Alternatively, you could use "CompileOptions" to pass in the "-g" setting and set "CleanIntermediate" to False so that intermediate files are kept.
Availability
Many Unix-type operating systems such as Linux or Mac OS X come with GCC already installed. If it is not present, then it can be added with a relatively simple configuration step.
Visual Studio
The CCompilerDriver package has been tested with several versions of the Microsoft Visual Studio compiler (http://www.microsoft.com/visualstudio) on a variety of different Windows platforms.
Tested versions of Microsoft Visual Studio.
The default "SystemCompileOptions" setting is "/O2", which gives optimized code. If you wanted to debug your code in a C language debugger, you could set the option "Debug" to be True. Alternatively, you could use "CompileOptions" to pass in the "/Zi" setting and set "CleanIntermediate" to False so that intermediate files are kept.
Availability
The Visual Studio compiler is often used as part of the Microsoft Visual Studio development environment. One way to obtain the compiler is through Microsoft Visual Studio Express suite, which is a free-of-charge lightweight version. For versions older than 2012 running on 64-bit Windows, you need the Microsoft .NET Framework 4 and the Windows SDK.
Compiling for 64-Bit Windows with Older Versions of Visual Studio Express and/or the Windows SDK
If you use a 64-bit version of Windows ($SystemID evaluates to "Windows-x86-64") and a Visual Studio Express 2010 or earlier, you will need to install the Microsoft .NET Framework 4 and the Windows SDK.
For most users, this means installing the Windows SDK for Windows 7. The "Windows 7" in the name merely indicates the most recent version of Windows that the SDK supports, and this SDK can be used with Windows Vista and Windows XP. Consult Microsoft, such as http://msdn.microsoft.com/en-us/windows/bb980924.aspx, if you are in doubt about which Windows SDK to install.
For Visual Studio Express 2010, you can use Version 7.1 of the Windows SDK, which is the most recent version as of this writing. This version was released in May 2010 and goes by the name "Microsoft Windows SDK for Windows 7 and .NET Framework 4". It is available for download from http://go.microsoft.com/fwlink/?LinkID=191420. Note that when you run the installer, you may see a warning dialog if you do not have Version 4 of the Microsoft .NET framework installed; it is recommended to acquire and install this first before using the Windows SDK installer.
For Visual Studio Express 2008, you need to use Version 7.0 of the Windows SDK. This version was released in August 2009 and goes by the name "Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 Service Pack 1". It is available for download from http://go.microsoft.com/fwlink/?LinkID=150217&clcid=0x409.
MinGW
The CCompilerDriver package has been tested with the MinGW native Windows port of GCC (http://www.mingw.org) on Windows platforms. Note that MinGW from this source does not produce 64-bit binaries. This capability is provided by MinGW-w64 described in the Generic Compiler section.
Tested versions of MinGW and platform.
The default "SystemCompileOptions" setting is "-O2", which gives optimized code without generating large files. If you wanted to debug your code in a C language debugger, you could set the option "Debug" to be True. Alternatively, you could use "CompileOptions" to pass in the "-g" setting and set "CleanIntermediate" to False so that intermediate files are kept.
Availability
MinGW is freely available through its main site, http://www.mingw.org.
Intel Compiler
$SystemID | Tested Versions of Intel C/C++ Compiler | |
Windows-x86-64 | 13.0 | |
Linux-x86-64 | 13.0 | |
MacOSX-x86-64 | 13.0 |
Availability
The Intel compiler is available as a commercial package for Windows, Linux, and Mac OS X on Intel architectures.
Note for Debian and Ubuntu Users
Users running Debian and Ubuntu will not be able to use CCompilerDriver if /bin/sh is soft-linked to the Debian Almquist shell (dash). This article from Intel advises to link to bash instead when installing the Intel compiler: http://software.intel.com/en-us/articles/using-intel-compilers-for-linux-with-ubuntu. Beyond that, the CCompilerDriver support for the Intel compiler also requires that /bin/sh is linked to bash.
Cygwin
The CCompilerDriver package has been tested with the Cygwin port of GCC on the Windows platform. Only versions of Cygwin GCC that support the -nocygwin option can be used to create Wolfram libraries. The -nocygwin option requires that you install the MinGW Cygwin package, and allows Cygwin GCC to create binaries that do not need the Cygwin DLL.
Availability
Cygwin is freely available through its main site, http://www.cygwin.com.
Generic C Compiler
The Generic C Compiler driver allows you to use virtually any C compiler that supports the common C compiler command line options. To use this compiler, you need to specify the compiler location. In addition, you may need to set the compiler name, options particular to the compiler, and script commands to set environment variables or the execution path.
MinGW for 64-Bit Targets
While the compiler available from http://www.mingw.org runs on both 32-bit and 64-bit versions of Windows, it currently compiles only 32-bit binaries. In order to produce 64-bit Wolfram Libraries, you can use a separate project called MinGW-w64 (available from http://mingw-w64.sourceforge.net). Here are the steps to do that.
First, you need to acquire and install the binaries from http://mingw-w64.sourceforge.net. In order to use this variant of MinGW on a 64-bit system, look for "w64" or "Win64" in the name, not "w32" or "Win32". A typical file name is mingw-w64-bin_x86-64-mingw_20100414.zip.
Extract the files in the zip archive file to your computer. In this example, assume they are in C:\MinGW-w64. The compiler will be in C:\MinGW-w64\bin and is called x86_64-w64-mingw32-gcc.exe.
To use the compiler, you need to first load the generic C compiler driver.
To test the compiler, try to build a simple executable that prints something.
If you find that your settings do not work, it might be useful to set the "ShellOutputFunction" option, which can display messages generated as the compilation runs. This is described in the section on Testing and Monitoring.
When you have built your program, you can run and test it.
To use this compiler whenever a C compiler is invoked, for example with Compile, you can make an assignment to $CCompiler.
An alternative way to set options to use when the Wolfram System compiler automatically uses a C compiler is to use Compile`$CCompilerOptions. For example, this is useful if you want to set options for debugging.
No Compiler
If you do not have a compiler installed, you will not be able to use the CCompilerDriver package. In this case a function such as CreateLibrary will issue an error message and return $Failed.