Debian Codegraph @

examples/ffi/cffi.lsp

raw
1 #|
2 Build and load this module with (compile-file "cffi.lsp" :load t)
3 |#
4 ;;
5 ;; This toplevel statement notifies the compiler that we will
6 ;; need this shared library at runtime. We do not need this
7 ;; statement in windows or macOS.
8 ;;
9 #-(or ming32 windows darwin)
10 (cffi:load-foreign-library "/usr/lib/libm.so")
11 ;;
12 ;; With this other statement, we import the C function sin(),
13 ;; which operates on IEEE doubles.
14 ;;
15 (cffi:defcfun ("sin" c-sin) :double :double)
16 ;;
17 ;; We now use this function and compare with the lisp version.
18 ;;
19 (format t "~%Lisp sin:~t~d~%C sin:~t~d~%Difference:~t~d"
20 (sin 1.0d0) (c-sin 1.0d0) (- (sin 1.0d0) (c-sin 1.0d0)))
21 ;;
22 ;; The following also works: no declaration!
23 ;;
24 (let ((c-cos (cffi:foreign-funcall "cos" :double 1.0d0 :double)))
25 (format t "~%Lisp cos:~t~d~%C cos:~t~d~%Difference:~t~d"
26 (sin 1.0d0) c-sin (- (sin 1.0d0) c-sin)))