# Working with multiple functions in LibraryLink

Posted 10 years ago
8181 Views
|
|
5 Total Likes
|
 In a previous post I explained how to work with complex numbers. In this post we are going to work with multiple LibraryLinkfunctions. The code below exports a 'get' and a 'set' function which either get or set an integer value.Here is the C code (GetAndSet.c):  #include "WolframLibrary.h"  mint value = 0;  DLLEXPORT int llGet(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {          MArgument_setInteger(Res,value);         return LIBRARY_NO_ERROR;}DLLEXPORT int llSet(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {        value = MArgument_getInteger(Args);        return LIBRARY_NO_ERROR;}The new concept in this code is a global variable 'value' which is initialized to '0' when the LibraryFunction is loaded.The llGet function takes no arguments and simple returns the current value for 'value'. The llSet function takes one integerargument and sets the value for 'value'.The following Wolfram Language code (GetAndSet.m) creates the library and loads the LibraryLink function:Needs["CCompilerDriver`"];lib = CreateLibrary[{"GetAndSet.c"},"GetAndSet"];LLGet = LibraryFunctionLoad[lib,"llGet",{},Integer];LLSet = LibraryFunctionLoad[lib,"llSet",{Integer},"Void"];Note that this time there are two calls to LibraryFunctionLoad, one for LLGet and one for LLSet (the Wolfram Language levelfunctions).Now we can try running this code in the Wolfram Language:  Mathematica 10.0 for Microsoft Windows (64-bit) Copyright 1988-2013 Wolfram Research, Inc.  In:= Get["GetAndSet.m"]  In:= LLGet[]  Out= 0In:= LLSetIn:= LLGet[]Out= 5In:= LLSetIn:= LLGet[]Out= 42Note how the initial value that LLGet returns is '0' and how this changes when LLSet changes the value.