Fasttrack to UFFI
I performed a small experiment, trying to track down a bug in a Common Lisp package that I'm porting to OpenMCL. I realized that some of my steps for tracking this down would probably make for a super simple tutorial on using UFFI
First, make a quick C library
lib.c:
#include <stdio.h>
int teststuff(int i) {
return i;
}
Now compile it:
$ gcc -g -c -dynamiclib -o lib.o lib.c
Now build a dynamic library:
$ libtool -dynamic -o lib.so lib.o -lc
Use ASDF to install UFFI
Now create a simple Lisp program that calls your function
test-uffi.lisp:
(asdf:operate 'asdf:load-op :uffi)
(uffi:load-foreign-library #p"/Users/stevej/lib.so" :supporting-libraries '("c"))
(uffi:def-function "teststuff" ((hello :int)) :returning :int)
(teststuff 1)
Voila, your first UFFI program.


![[Atom Enabled]](http://saladwithsteve.com/valid-atom.png)
4 Comments:
This is very helpful, thank you. I'm currently tackling UFFI and finding a paucity of tutorial material.
6:57 AM
That's great to hear. I had the same problem when learning UFFI.
12:35 PM
Very helpful. The two tutorials I've found are too involved, this is simple and to the point, even though you wrote it quite a while ago.
The code you posted for compilation gave some wacky results, so here's what I got to work for that part.
$gcc -c -o lib.o lib.c
$gcc -shared -o lib.so lib.o
And after that it worked fine.
7:24 AM
I'm sorry, OpenMCL is for the Mac so I gave Mac specific instructions for building a shared library.
It sounds like you're on linux so thanks for sharing!
7:50 AM
Post a Comment
Links to this post:
Create a Link
<< Home