Search code examples
lispcommon-lispffisbcl

common lisp sbcl manual ffi example failed


I read sbcl manual,and have a problem at the 8.5 Foreign Data Structure Examples chapter.

I use following examples to verify whether it can run correct.

_______________________________________________________________

Or consider this example of an external C variable and some accesses:

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;

which can be manipulated in Lisp like this:

(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))

________________________________________________________________

Now I run above example code on slime,and it signaled a error.

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.***

What should I do to define such a struct that can contain it's self point.


Solution

  • Note that the manual also says:

    Types may be either named or anonymous. With structure and union types, the name is part of the type specifier, allowing recursively defined types such as:

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

    I haven't used SBCL's FFI, but I'd guess that means that you should use:

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