Month: August 2013

A small example of how to use the code generated with TalkFFI

Posted on Updated on

Hi!
Well, we know generate code with talkffi, now I will show how it is used.

Planning.

1) Create a very small example code C
2) Generate code with TalkFFI
3) View de Results
4) Use the bindings

1) Example in C

-In a folder create a file ‘example.c’

-Write code, for example:

int sum(int a, int b){return a+b;}

-Create file example.h, and declare the functions that you’ll would used, in this case:

int sum(int a, int b);

-Generate dynamic library.

#compile $gcc -m32 -c example.c
#generate .so, .dll or dylib according to the system
$gcc -shared -m32 -o example.so example.o

2) Generate code with TalkFFI

Follow the steps in the previous tutorial[1]

3) View the Results.

TalkFFI generate two packages in Pharo: *-FFI-Bindings and *-FFI-Types.

*The first contains at least two class: the Map class, and the H class.

The function of the Map class is provide the utilities to comunicate with NativeBoost; and the H’s class, contain the callback functions that it define.

*The second packages contains the definition of the external structs, external enum and native boost callbacks

4) Use the bindings.

To use, you need modify the mehod libraryNameOrHandle in the class *Map with your example.so path. Then in a workspace write this.

ExampleMap inicialize.
#and call the function you need
ExampleH uniqueInstance sum: 2 b: 1.

[1] https://rochiamaya.wordpress.com/2013/07/30/create-bindings-with-talkffi/