在Common Lisp中使用CFFI,调用函数返回指针

时间:2018-03-08 22:24:36

标签: common-lisp sbcl cffi

我正在使用SBCL,并尝试使用CFFI。我开始关注手册here,但我一直认为函数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启发了这个实验。

1 个答案:

答案 0 :(得分:3)

作为CFFI:DEFCFUN的字符串给出的外来名称将使用泛型函数CFFI:TRANSLATE-NAME-FROM-FOREIGN转换为lispier名称。默认行为是将下划线更改为连字符:

CL-USER> (cffi:translate-name-from-foreign "mylib_fun_name" *package*)
MYLIB-FUN-NAME

您还可以在函数定义中明确指定lisp名称:

(defcfun ("mylib_fun_name" fun-name) ...)
相关问题