我正在使用 SBCL,并尝试使用 CFFI。我开始按照这里的手册进行操作,但我一直认为这个函数curl_easy_init
是未定义的,所以我决定尝试一个更简单的案例。
一些函数按预期工作,而另一些函数似乎没有定义。这在下面解释:
这是我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
/* "Works" except output doesn't appear. Not important for now */
void hello()
{
printf("Hello World\n");
}
/* Works as expected */
float add(float x1, float x2)
{
return x1 + x2;
}
/* appears to not be defined in SBCL*/
void* init_this()
{
return malloc(10);
}
/* Appears to not be defined in SBCL*/
void clean_this(void* ptr)
{
free(ptr);
}
此 C 文件编译为:
gcc -shared -o cffi_test.so -fPIC cffi_test.c
我的 Common Lisp 代码:
(quicklisp:quickload "cffi")
(defpackage :my-cffi
(:use :common-lisp :cffi))
(in-package :my-cffi)
(export '(hello add init_this clean_this))
(define-foreign-library cffi_test
(t (:default "~/Work/clisp/cffi_test")))
(use-foreign-library cffi_test)
(defcfun "hello" :void)
(defcfun "add" :float (x1 :float ) (x2 :float))
(defcfun "init_this" :pointer)
(defcfun "clean_this" :void (handle :pointer))
我得到了我所期望的:
* (my-cffi:add 3.0 4.0)
7.0
什么似乎是一个问题:
(my-cffi:init_this)
生成错误:
The function MY-CFFI:INIT_THIS is undefined.
[Condition of type UNDEFINED-FUNCTION]
...
Backtrace:
0: (SB-IMPL::RETRY-%COERCE-NAME-TO-FUN MY-CFFI:INIT_THIS NIL)
Locals:
SB-IMPL::NAME = MY-CFFI:INIT_THIS
SB-IMPL::STRICTLY-FUNCTIONP = NIL
我得到了同样的错误clean_this
,并且在上面的链接中,按照手册,curl_easy_init
这激发了这个实验。