Message Boards Message Boards

5
|
8935 Views
|
1 Reply
|
5 Total Likes
View groups...
Share
Share this post:

Working with multiple functions in LibraryLink

Posted 11 years ago
In a previous post I explained how to work with complex numbers. In this post we are going to work with multiple LibraryLink
functions. 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[0]);
        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 integer
argument 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 level
functions).

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[1]:= Get["GetAndSet.m"]
 
 In[2]:= LLGet[]
 
 Out[2]= 0

In[3]:= LLSet[5]

In[4]:= LLGet[]

Out[4]= 5

In[5]:= LLSet[42]

In[6]:= LLGet[]

Out[6]= 42


Note how the initial value that LLGet returns is '0' and how this changes when LLSet changes the value.
POSTED BY: Arnoud Buzing
Posted 8 years ago

Working with multiple functions in LibraryLink

POSTED BY: Shutao Tang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract