【问题标题】:common lisp sbcl manual ffi example failedcommon lisp sbcl 手册 ffi 示例失败
【发布时间】:2016-05-22 12:35:23
【问题描述】:

我阅读了sbcl manual,并在 8.5 外部数据结构示例一章遇到了问题。

我使用以下示例来验证它是否可以正确运行。

_______________________________________________________________

或者考虑这个外部 C 变量和一些访问的例子:

struct c_struct {
    short x, y;
    char a, b;
    int z;
    c_struct *n;
};


extern struct c_struct *my_struct;
my_struct->x++;
my_struct->a = 5;
my_struct = my_struct->n;

在 Lisp 中可以这样操作:

(define-alien-type nil
  (struct c-struct
          (x short)
          (y short)
          (a char)
          (b char)
          (z int)
          (n (* c-struct))))
(define-alien-variable "my_struct" (* c-struct))
(incf (slot my-struct 'x))
(setf (slot my-struct 'a) 5)
(setq my-struct (slot my-struct 'n))

________________________________________________________________

现在我在 slime 上运行上面的示例代码,它发出错误信号。

unknown alien type: C-STRUCT
  [Condition of type SIMPLE-ERROR]
  Restarts:
  0: [RETRY] Retry SLIME REPL evaluation request.
  1: [ABORT] Return to sldb level 7.
  2: [RETRY] Retry SLIME REPL evaluation request.
  3: [ABORT] Return to sldb level 6.
  4: [RETRY] Retry SLIME REPL evaluation request.
  5: [ABORT] Return to sldb level 5.***

我应该怎么做才能定义这样一个可以包含它的自身点的结构。

【问题讨论】:

    标签: lisp common-lisp ffi sbcl


    【解决方案1】:

    请注意,手册还说:

    类型可以是命名的也可以是匿名的。对于结构和联合类型,名称是类型说明符的一部分,允许递归定义类型,例如:

    (struct foo (a (* (struct foo))))

    我没有使用过 SBCL 的 FFI,但我猜这意味着你应该使用:

    (define-alien-variable "my_struct" (* (struct c-struct)))
    

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 2015-09-30
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多