Next: , Previous: Tutorial-Getting a URL, Up: Tutorial


4.3 Loading foreign libraries

First of all, we will create a package to work in. You can save these forms in a file, or just send them to the listener as they are. If creating bindings for an ASDF package of yours, you will want to add :cffi to the :depends-on list in your .asd file. Otherwise, just use the asdf:oos function to load CFFI.

  (asdf:oos 'asdf:load-op :cffi)
   
  ;;; Nothing special about the "CFFI-USER" package.  We're just
  ;;; using it as a substitute for your own CL package.
  (defpackage :cffi-user
    (:use :common-lisp :cffi))
   
  (in-package :cffi-user)
   
  (define-foreign-library libcurl
    (:darwin (:or "libcurl.3.dylib" "libcurl.dylib"))
    (:unix (:or "libcurl.so.3" "libcurl.so"))
    (t (:default "libcurl")))
   
  (use-foreign-library libcurl)

Using define-foreign-library and use-foreign-library, we have loaded libcurl into Lisp, much as the linker does when you start a C program, or common-lisp:load does with a Lisp source file or FASL file. We special-cased for unix machines to always load a particular version, the one this tutorial was tested with; for those who don't care, the define-foreign-library clause (t (:default "libcurl")) should be satisfactory, and will adapt to various operating systems.