In a file called lib.c:
int addTwo(int x, int y) {
return x + y;
}
Compiled on the Powerbook with:
gcc -dynamiclib -o lib.dylib lib.c
Then, in DrScheme v299:
(require (lib "foreign.ss")) (define addTwo (get-ffi-obj "addTwo" "lib" (_fun _int _int -> _int)))
And, in the interactions window, I can now use:
> (addTwo 2 2) 4
That is so very sweet. Dynamic FFI. Thank you, Eli.
PS. The cool bit is the definition of ‘addTwo’ in Scheme; it’s a function name, a library name, and a type contract. How sweet is that?!? This makes it so much easier to solve a problem I’ve been having with native number types from PLT Scheme. So much goodness, it hurts.
Shouldnt that be (addTwo 5 3) ?
Absolutely correct. And the result should be ’8′.
I’m ashamed for having presented an example that doesn’t result in the number 8. Alas, I set a poor example.