Created
October 18, 2009 02:53
-
-
Save lamberta/212533 to your computer and use it in GitHub Desktop.
use C shared lib with cffi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "greeting.h" | |
void hello (char* name) { | |
printf("Hello, %s!\n", name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void hello (char*); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mymath.h" | |
int double_it (int n) { | |
return n * 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int double_it (int); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment