Creating a Library

The CCompilerDriver package lets you work with C compilers that are installed on your computer. It is used automatically by the Wolfram System compiler when you set the option CompilationTarget to "C", but you can use it to build your own executables, libraries, and object files from source code written in the C language.

This section discusses some of the ways that you can use these tools to create libraries. The main function to create a library is CreateLibrary. These examples will use SymbolicC.

First, the packages are loaded.

This creates a basic C function.

This compiles the code into a library.

If the input came from one or more files of C code, these could be given to CreateLibrary in a list.

The extension for the generated library is added to be compatible with the type of library being created.

On Windows, the stub LIB file and export EXP files are also created.

More information on options and settings is found in the section on Configuring the Compilation.

Wolfram Libraries

The Wolfram Language kernel supports loading dynamic libraries so that functions can be called directly. This is described in the documentation for Wolfram LibraryLink. The CCompilerDriver package is useful for building these libraries since you can carry out much of the work from within the Wolfram Language.

The main header file for Wolfram Libraries contains some definitions that make it easier to write C code that works on different platforms.

The CCompilerDriver package provides some sample C code. If you have a C compiler installed on your machine you can work with the samples, as shown in the documentation.

First, you have to load the package.

This finds a sample source file in the package and prints it out.

This creates a library and returns the full path.

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

This library was written as a Wolfram Library, so you can load functions from it using LibraryFunctionLoad.

This calls the LibraryFunction.

There are also examples in the Wolfram LibraryLink documentation.