Introduction

Most modern computer systems provide ways to collect code into libraries. These libraries are said to be dynamic if they can be loaded into an application at runtime rather than when the application is built. If loading can happen after an application has already started running, it is a particularly useful way to add functionality. Many plug-in architectures are built from dynamic libraries that are loaded in this way.

Wolfram LibraryLink allows dynamic libraries to be directly loaded into the Wolfram Language kernel so that functions in the libraries can be immediately called from the Wolfram Language. You can exchange not only C-like data types such as integers, reals, packed arrays, and strings, but also arbitrary Wolfram Language expressions. In addition, there are useful functions such as sending errors and calling back to the Wolfram Language.

You can load a function from a Wolfram Library into the Wolfram Language with LibraryFunctionLoad.

You call the LibraryFunction, giving it an integer argument; the result is also an integer.

You can use the function inside a table or other Wolfram Language programming structure.

If you call the function with an input that is not an integer, then an error results and the input is returned unchanged.

One way to create a Wolfram Library is to write it in C or C++ and use C development tools. Here is the source for the function (the details of the C code are explained in the section "Library Structure and Life Cycle").

DLLEXPORT int demo_I_I(WolframLibraryData libData, 
                mint Argc, MArgument *Args, MArgument Res) {
    mint I0;
    mint I1;
    I0 = MArgument_getInteger(Args[0]);
    I1 = I0 + 1;
    MArgument_setInteger(Res, I1);
    return 0;
}

In addition to passing machine integers, a number of other formats can be passed, including machine reals and complexes, strings, packed arrays, sparse arrays, numeric arrays, images, and general expressions.

Alternatives to Wolfram Library Functions

Loading functions directly from a Wolfram Library has a number of advantages and disadvantages. This section reviews the advantages and disadvantages and discusses alternatives.

The Wolfram Language

One alternative is to use the Wolfram Language. This means writing code in the normal way for programming the Wolfram Language. Following is a summary of the advantages and disadvantages.

Foreign Function Interface »

The Foreign Function Interface (FFI) allows you to call functions from C-compatible dynamic libraries directly from Wolfram Language. Following is a summary of the advantages and disadvantages.

The Wolfram Compiler »

A lower level alternative is to use the Wolfram Compiler. This means writing Wolfram Language code and compiling it into efficient native machine code. Following is a summary of the advantages and disadvantages.

WSTP Applications

Another alternative is to use the Wolfram Symbolic Transfer Protocol (WSTP). This means writing code as a C program and connecting to the Wolfram Language using the WSTP programming interface. Following is a summary of the advantages and disadvantages.