Search code examples
ccommon-lispecl

How can I return a string from a call to ffi:c-inline in ecl?


How can I return a string constant from a call to ffi:c-inline?

I've tried variations of the following without success (ORGANIZATION is a constant defined in constants.h):

(ffi:clines "#include \"./constants.h\"")
(ffi:c-inline () () :string "ORGANIZATION" :one-liner t)

The example above results in the following compiler error:

Unknown representation type :STRING


Solution

  • Use :cstring instead of :string:

    constants.h:

    #define ORGANIZATION "foobar"
    

    ecl.lsp:

    (ffi:clines "#include \"./constants.h\"")
    (defun myfun ()
      (ffi:c-inline () () :cstring "ORGANIZATION" :one-liner t))
    

    From ecl prompt:

    > (compile-file "ecl.lsp" :load t)
    ...
    > (myfun)
    
    "foobar"
    > 
    

    Reference: https://ecl.common-lisp.dev/static/manual/Foreign-Function-Interface.html#Primitive-Types