C CODE GENERATOR TUTORIAL
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.
You can use the compiled function like any other Wolfram Language function, passing it real number input.
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.
More detailed information can be found in the compiler documentation.
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.
Now you can start to build up elements of a C program. The following represents a variable declaration.
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.
You can keep the function as an expression, or you can convert it into a string that shows the C code.
More detailed information can be found in the SymbolicC documentation.
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.
Now you can create a DLL from a file of C code.
This is a sample source file.
This creates a shared library, returning the full path.
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 DLLFunctionLoad.
You can call the library function from the Wolfram Language.
More detailed information can be found in the CCompilerDriver documentation.