Creating an Executable

The CCompilerDriver package lets you work with C compilers that are installed on your computer. It is used automatically by the Mathematica compiler when you set the option CompilationTarget to "C", but you can use it to build your own executables, libraries, and object files from source code written in the C language.

First, the packages are loaded.

In[1]:=
Click for copyable input

This creates a basic C function.

In[77]:=
Click for copyable input
Out[78]=

This compiles the code into an executable.

In[5]:=
Click for copyable input
Out[5]=

If the input came from one or more files of C code, these could be given to CreateExecutable in a list.

The extension for the generated executable is added to be compatible with the system.

If an input file is given that has an extension of .tm, CreateExecutable will treat it as a MathLink template file and use mprep to process it.

More information on options and settings is found in the section on Configuring the Compilation.

MathLink Executables

MathLink is Mathematica's unique high-level symbolic interface standard for interprogram communication. The CCompilerDriver package is useful for working with MathLink since you can build MathLink programs from within Mathematica. One particularly useful feature is that if an input file is given with an extension of .tm, CreateExecutable will treat this as a MathLink template file and use mprep to process it.

The MathLink developer kit that ships with Mathematica contains a number of examples. The location for these is different depending on the platform.

Under Windows, the location is as follows.

In[21]:=
Click for copyable input

Under Linux, the location is as follows.

In[21]:=
Click for copyable input

Under Mac OS X, the location is as follows.

In[21]:=
Click for copyable input

Now you can build these with the CCompilerDriver package; first you have to load it.

In[1]:=
Click for copyable input

This finds a sample source file in the package and prints it out.

In[24]:=
Click for copyable input
Out[24]=
In[25]:=
Click for copyable input
Out[25]=
In[26]:=
Click for copyable input
Out[26]=

More information is found in the MathLink documentation.

New to Mathematica? Find your learning path »
Have a question? Ask support »