Related Technologies
This feature is not supported on the Wolfram Cloud.

Code generation from the Wolfram Language involves converting programs written in the Wolfram Language into other languages and then supporting them so that they can be executed. The Wolfram System compiler provides a system for code generation into the C language.

This section describes some technologies that are related to code generation.

The Wolfram System Compiler

The Wolfram System compiler is an important way both to speed up and also to work with Wolfram Language computations. It does this by taking assumptions about the computations and rewriting them in more efficient ways.

A key way to use the Wolfram System compiler is with the function Compile.

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

You can use the compiled function like any other Wolfram Language function, passing it real number input.

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

However, if you pass the compiled function an input that is not a real number, an error message is returned. The Wolfram Language still returns a result, but has not used the compiler.

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

More detailed information can be found in the compiler documentation.

SymbolicC

SymbolicC is a key part of the Wolfram Language's C code generation system. It is used automatically, when Export or ExportString to C code is used. It provides a hierarchical view of C code as Wolfram Language expressions. This makes it well suited to creating, manipulating, and optimizing C code.

You can use SymbolicC for your own code-generation purposes. It is also used extensively for the Wolfram Language's code generation tools.

To use SymbolicC, you first need to load the package.

In[1]:=
Click for copyable input

Now you can start to build up elements of a C program. The following represents a variable declaration.

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

An important feature of SymbolicC expressions is that they are inert; they evaluate to themselves, staying in an unevaluated form.

Here is an assignment and an entire function.

In[3]:=
Click for copyable input

You can keep the function as an expression, or you can convert it into a string that shows the C code.

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

More detailed information can be found in the SymbolicC documentation.

CCompilerDriver

CCompilerDriver is a Wolfram Language package that gives an interface to C compilers installed on your computer. It can build shared libraries, executables, and object files. Since it is integrated with the Wolfram Language, it is easy to use it for creating C code and linking this back in with the Wolfram Language, for example, giving a convenient way to test the code.

Of course, you may choose to set up your own project or makefiles and not use the C Compiler Driver tools.

To use the package, it must first be loaded.

In[1]:=
Click for copyable input

Now you can create a DLL from a file of C code.

This is a sample source file.

In[2]:=
Click for copyable input

This creates a shared library, returning the full path.

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

If you do not have any suitable C compiler, the compilation will fail and a message will be generated.

The sample source file can work as a Wolfram Library and can be loaded with .

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

You can call the library function from the Wolfram Language.

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

More detailed information can be found in the CCompilerDriver documentation.