Next: , Up: Variables


defcvar

Syntax

— Macro: defcvar name type &key read-only => lisp-name

name ::= lisp-name | foreign-name | (foreign-name lisp-name)

Arguments and Values

foreign-name
A string denoting a foreign function.
lisp-name
A symbol naming the Lisp function to be created.
type
A foreign type.
read-only
A boolean.

Description

The defcvar macro

When one of lisp-name or foreign-name is omitted, the other is automatically derived using the following rules:

Examples

  CFFI> (defcvar "errno" :int)
  => *ERRNO*
  CFFI> (foreign-funcall "strerror" :int *errno* :string)
  => "Inappropriate ioctl for device"
  CFFI> (setf *errno* 1)
  => 1
  CFFI> (foreign-funcall "strerror" :int *errno* :string)
  => "Operation not permitted"

Trying to modify a read-only foreign variable:

  CFFI> (defcvar ("errno" +error-number+) :int :read-only t)
  => +ERROR-NUMBER+
  CFFI> (setf +error-number+ 12)
  ;; error--> Trying to modify read-only foreign var: +ERROR-NUMBER+.
  

Note that accessing errno this way won't work with every C standard library.

See Also

get-var-pointer