COMPILED FUNCTION TOOLS TUTORIAL

Code Generation

Code generation from the Wolfram System 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, which 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.

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