The FLI provides the macro define-foreign-function for creating interfaces to foreign functions. It takes the name of the function you wish to interface to, the argument types the function accepts, and the result type the function returns.
Given the following C declaration to
float FahrenheitToCelsius( int );
The FLI interface is as follows:
(fahrenheit-to-celsius "FahrenheitToCelsius" :source)
The first argument to define-foreign-function declares that
is the name of the Lisp function that is generated to interface with the C function
keyword is a directive to the define-foreign-function name mangler that Fahrenheit
is the name of the C function as seen in the source files. On some platforms the actual symbol name available in the foreign object file we are inferfacing with could include character prefixes such as "
" and "
", and so the
keyword encoding allows you to write cross-platform portable foreign language interfaces.
The second argument to define-foreign-function,
, is the argument list for the foreign function. In this case, only one argument is required. The first part of each argument descriptor is the lambda argument name. The rest of the argument describes the type of argument we are trying to interface to and how the conversion from Lisp to C is performed. In this case the foreign type
specifies that we are interfacing between a Lisp integer and a C type "int".
keyword tells us that the conversion required between the C function and Lisp uses the foreign type
. This tells Lisp that C will return a result of type "float", which needs to be converted to a Lisp single-float.
The final keyword argument,
, specifies which language the foreign function was written in. In this case the example uses ANSI C. This keyword determines how single-floating point values are passed to and returned from C functions as described for define-foreign-function.
LispWorks Foreign Language Interface User Guide and Reference Manual - 7 Dec 2011