Hi Juan,

On Tue, May 21, 2013 at 6:13 PM, Juan Jose Garcia-Ripoll <juanjose.garciaripoll@gmail.com> wrote:

The CFFI model is simply too rigid: they assume that libraries can be loaded at run time, always. Instead ECL supports also a model in which libraries are linked with the compiled code, being available that way. I fear there is no way to work around this but using ECL's own FFI mingled with CFFI

> (ql:quickload :cffi)
> (compile-file "foo.lsp" :load t)

Here what I get when trying this on windows:

 $ /cygdrive/c/Users/dietrich/home/cs/lang/lisp/ecl/64/git/install/ecl.exe
;;; Loading "E:/Users/dietrich/home/cs/lang/lisp/ecl/ecl/64/git/library/quicklisp/install/setup.lisp"
;;; Loading #P"C:/Users/dietrich/home/cs/lang/lisp/ecl/64/git/install/asdf.fas"
ECL (Embeddable Common-Lisp) 13.4.1 (git:0e93edfc7aefe16bb5b0fc20d4735ae69ac346ce)
Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya
Copyright (C) 1993 Giuseppe Attardi
Copyright (C) 2000 Juan J. Garcia-Ripoll
ECL is free software, and you are welcome to redistribute it
under certain conditions; see file 'Copyright' for details.
Type :h for Help.  
Top level in: #<process TOP-LEVEL>.
> (ql:quickload :cffi)
To load "cffi":
  Load 1 ASDF system:
; Loading "cffi"

> (compile-file "foo.lsp" :load t)

;;; Compiling foo.lsp
;;; Compiling #<input stream foo.lsp>
;;; Loading "E:/Users/dietrich/home/cs/lang/lisp/ecl/ecl/64/git/diary/2013/05/22/cffi-user/foo.fasc"

Condition of type: SIMPLE-ERROR
In interpreted code, attempted to call a foreign function
 #<foreign POINTER-VOID>
but ECL was built without support for that.
Available restarts:

1. (RESTART-TOPLEVEL) Go back to Top-Level REPL.

Broken at CFFI-USER::MYCOS. In: #<process TOP-LEVEL>.
 File: #P"E:/Users/dietrich/home/cs/lang/lisp/ecl/ecl/64/git/diary/2013/05/22/cffi-user/foo.lsp" (Position #317)
>> :q

Top level in: #<process TOP-LEVEL>.
> (quit)

How can I build ECL to support calling a foreign function?

Thanks a lot,


PS:  The code works on darwin when I change '#+ecl' to '#+(and ecl (not darwin))' as the -lm option is enough and (ffi:load-foreign-library "/usr/lib/libm.dylib" :system-library nil) shouldn't be read; on Windows I used #+(and ecl (not darwin) (not windows)) to avoid the reading of (ffi:load-foreign-library "/usr/lib/libm.dylib" :system-library nil).


;;; 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 libm
      (:unix (:or "libm.so" "libm.dylib"))
    (t (:default "libm")))
  (use-foreign-library libm))

(ffi:load-foreign-library "/usr/lib/libm.dylib" :system-library nil)

;;; Initialize libcurl with FLAGS.
(defcfun (mycos "cos") :double (value :double))

(format t "C:    cos(~e) = ~g~%" 3.2d0 (mycos 3.2d0))
(format t "Lisp: cos(~e) = ~g~%" 3.2d0 (cos 3.2d0))

Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28006 (Spain)