The C Preprocessor

SymbolicC has a number of functions for working with the C preprocessor. These allow you to set up including header files, defining macros, as well as setting up conditional compilation.

Working with Header Files

First, you need to load the package.

Needs["SymbolicC`"]

Now you can include a header file with (PackageLink[CInclude, paclet:SymbolicC/ref/CInclude, SymbolicC Package Symbol]).

CInclude["exports.h"]//ToCCodeString

If it is a system header file, this is dealt with appropriately.

CInclude["<foo.h>"]//ToCCodeString

You can add a list of header files in one (PackageLink[CInclude, paclet:SymbolicC/ref/CInclude, SymbolicC Package Symbol]) expression.

CInclude[{"<math.h>","<stdlib.h>"}]//ToCCodeString

It is also possible to use (PackageLink[CComment, paclet:SymbolicC/ref/CComment, SymbolicC Package Symbol]) to add a comment to an include statement.

{CInclude["exports.h"],CComment["Import the exports"]}//ToCCodeString

Defining Macros

You can define a macro using (PackageLink[CDefine, paclet:SymbolicC/ref/CDefine, SymbolicC Package Symbol]).

First, you need to load the package.

Needs["SymbolicC`"]

Now you can define a macro.

CDefine["DEBUG"]//ToCCodeString

You can combine the definition with other SymbolicC functions.

CDefine[CCall[ addMacro,{a,b}], COperator[Plus, {a,b}]]//ToCCodeString

You can also add a comment by adding a (PackageLink[CComment, paclet:SymbolicC/ref/CComment, SymbolicC Package Symbol]) expression.

CDefine["DEBUG", CComment[ "Turn on debugging"]]//ToCCodeString

Conditional Compilation

There are a range of functions to create C preprocessor conditional compilation output.

First, you need to load the package.

Needs["SymbolicC`"]

Now you can create a conditional compilation output.

CPreprocessorIfdef["DEBUG"]//ToCCodeString

You can also add an entire command.

CPreprocessorIfdef[COperator[Equal,{"a",1}]]//ToCCodeString

(PackageLink[CPreprocessorIfdef, paclet:SymbolicC/ref/CPreprocessorIfdef, SymbolicC Package Symbol]) can create an entire sequence.

CPreprocessorIfdef["DEBUG64",CAssign[size,64], CAssign[size,32]]//ToCCodeString

The conditional compilation output commands are summarized in the following table.

C preprocessor conditional compilation output.