ffi_open(libname)
Return an object representing a "handle" to the shared library file named libname
, false
if the shared library could not be loaded.
Imagine we want our Slogan program to call the function defined in the following C program:
// test.c
#include <stdio.h>
#include "slogan.h"
___slogan_obj say_hello(___slogan_obj *args)
{
___slogan_obj message;
char *s = "hello from C!";
char *p;
___slogan_obj_to_charstring(args[0], &p);
printf("Slogan says \"%s\"\n", p);
___charstring_to_slogan_obj(s, &message);
return message;
}
This can be compiled to a shared library on Linux by,
$ gcc -Wall -shared -fPIC -I ${SLOGAN_HOME}/src/include/-I ${SLOGAN_HOME}/platform/gsc/include \
-o test.so test.c
and on OS X by,
$ gcc -dynamiclib -Wl,-undefined -Wl,dynamic_lookup -I ${SLOGAN_HOME}/src/include/ \
-I ${SLOGAN_HOME}/platform/gsc/include -o test.so test.c
After the shared library test.so
is generated, the function say_hello
can be invoked from Slogan as shown below:
let clib = ffi_open("./test.so")
let f = ffi_fn(clib, "say_hello")
ffi_call_obj(f, #["hello from Slogan"])
//> Slogan says "hello from Slogan"
// hello from C!
ffi_close(clib)
// true