Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 4: An Introduction to Foreign Interfaces and <strong>CFFI</strong> 6<br />
(asdf:oos ’asdf:load-op :cffi)<br />
;;; Nothing special about the "<strong>CFFI</strong>-USER" package. We’re just<br />
;;; using it as a substitute for your own CL package.<br />
(defpackage :cffi-user<br />
(:use :common-lisp :cffi))<br />
(in-package :cffi-user)<br />
(define-foreign-library libcurl<br />
(:darwin (:or "libcurl.3.dylib" "libcurl.dylib"))<br />
(:unix (:or "libcurl.so.3" "libcurl.so"))<br />
(t (:default "libcurl")))<br />
(use-foreign-library libcurl)<br />
Using define-foreign-library and use-foreign-library, we have loaded libcurl<br />
into <strong>Lisp</strong>, much as the linker does when you start a C program, or common-lisp:load does<br />
with a <strong>Lisp</strong> source file or FASL file. We special-cased for unix machines to always load<br />
a particular version, the one this tutorial was tested with; for those who don’t care, the<br />
define-foreign-library clause (t (:default "libcurl")) should be satisfactory, and<br />
will adapt to various operating systems.<br />
4.4 Initializing libcurl<br />
After the introductory matter, the tutorial goes on to present the first function you should<br />
use.<br />
CURLcode curl_global_init(long flags);<br />
Let’s pick this apart into appropriate <strong>Lisp</strong> code:<br />
;;; A CURLcode is the universal error code. curl/curl.h says<br />
;;; no return code will ever be removed, and new ones will be<br />
;;; added to the end.<br />
(defctype curl-code :int)<br />
;;; Initialize libcurl with FLAGS.<br />
(defcfun "curl_global_init" curl-code<br />
(flags :long))<br />
Implementor’s note: By default, <strong>CFFI</strong> assumes the UNIX viewpoint that there is<br />
one C symbol namespace, containing all symbols in all loaded objects. This is not<br />
so on Windows and Darwin, but we emulate UNIX’s behaviour there. [defcfun],<br />
page 89 for more details.<br />
Note the parallels with the original C declaration. We’ve defined curl-code as a wrapping<br />
type for :int; right now, it only marks it as special, but later we will do something<br />
more interesting with it. The point is that we don’t have to do it yet.<br />
Looking at ‘curl.h’, CURL_GLOBAL_NOTHING, a possible value for flags above, is defined<br />
as ‘0’. So we can now call the function: