Skip to content

Instantly share code, notes, and snippets.

@lamberta
Created October 18, 2009 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamberta/212533 to your computer and use it in GitHub Desktop.
Save lamberta/212533 to your computer and use it in GitHub Desktop.
use C shared lib with cffi
;;;building a C shared library,
;;;and calling it in Common Lisp with CFFI
(require :asdf)
(asdf:load-system :cffi)
(defpackage :cffi-user
(:use :common-lisp :cffi))
(in-package :cffi-user)
(define-foreign-library libmystuff
(t (:default "libmystuff")))
(use-foreign-library libmystuff)
(defcfun "hello" :void (name :pointer))
(defcfun "double_it" :int (n :int))
;;use it
(let ((n 4))
(format t "~a double it! ~a~%" n (double-it 4)))
(with-foreign-string (name "Billy")
(hello name))
#compile with 'position independent code' flag
gcc -fpic -c greeting.c mymath.c
#build the shared object library
gcc -shared -o libmystuff.so greeting.o mymath.o
#copy library somewhere we can see it, see /etc/ld.so.conf
cp libmystuff.so ~/local/lib
#configure linker to see it
sudo ldconfig
#include <stdio.h>
#include "greeting.h"
void hello (char* name) {
printf("Hello, %s!\n", name);
}
void hello (char*);
#include "mymath.h"
int double_it (int n) {
return n * 2;
}
int double_it (int);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment