clever-lang/clever — Issue #101: A "Hello FFI" test to ilustrate how to use the Clever FFI 
Author: gogo40
Created: 2012-02-11 22:41:31+00:00
State: open

Hi, i wrote an example to show how to use the Clever FFI. In this example, we build an shared library and call some "C functions" using Clever.

First, we write a C code with the function that we want to export:

```
/*
hello.c
A simple C code to test FFI support on Clever.
*/

#include

void hello() {
printf("Hello world, i am an alien!\n");
}

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

```
After, we write the Clever script:

```
//FFI_hello.clv
//A Clever script to test FFI support.

import std;

[hello,v] hello ();

c:=[hello,i]add(1,1);

println(c);

```

And finally, we build the shared library and run the clever script:

```
#A script to build a shared library to test FFI support on Clever.
gcc -fPIC -O3 -shared -c hello.c
gcc -shared -Wl,-soname,hello.so -o hello.so hello.o -lc -ldl

set LD_LIBRARY_PATH=.

./clever -f FFI_hello.clv

```

What do you think?