Introduction

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.

One mode of use is to create C code that conforms to a Wolfram Library; this can be compiled with a C compiler and linked back into the Wolfram Language. This is how the CompilationTarget option of Compile works when it is set to "C"; it is described in more detail in the tutorial on CompilationTarget.

An alternative is to generate C code that can be used outside of the Wolfram Language. Since this will execute outside of the Wolfram Language, any external calls from the compiler cannot be supported. This means that high-level Wolfram Language functions such as Integrate or NDSolve that really do need the Wolfram Language are not supported. On the other hand, if your code is based on the set of the Wolfram Language that can be processed by the compiler, this can work outside of the Wolfram Language. This is done by a runtime library that is provided to give support to the C code.

One way to use the code generator is to call Export and ExportString. The following creates a fragment of C code from a Wolfram Language function; this could be compiled into a shared library.

Export returns both the C file and the C header file. You can inspect the contents of the C file.

Looking at this code, there are a number of issues that need to be resolved in order to use it. This includes a library to provide implementations for functions such as the creation of the WolframLibraryData. These issues are discussed in other sections of this tutorial.