Next Previous Up Top Contents Index

1.1 A simple example of how to interface a foreign function

1.1.1 Defining the FLI function

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 toFarenheitToCelsius:

float FarenheitToCelsius( int );

The FLI interface is as follows:

(fli:define-foreign-function 
    (farenheit-to-celsius "FarenheitToCelsius" :source)
    ((farenheit :int))
  :result-type :float
  :language :ansi-c
  )

The first argument todefine-foreign-function declares thatfarenheit-to-celsius is the name of the Lisp function that is generated to interface with the C functionFarenheitToCelsius. The:source keyword is a directive to thedefine-foreign-function name mangler thatFarenheitToCelsius 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:source keyword encoding allows you to write cross-platform portable foreign language interfaces.

The second argument tofine-foreign-function,((farenheit :int)), 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 ot interface to and how the conversion from Lisp to C is performed. In this case the foreign type:int specifies that we are interfacing between a Lisp integer and a C type "int".

The:result-type keyword tells us that the conversion required between the C function and Lisp uses the foreign type:float. 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,:language, specifies which language the foreign function was written in. In this case the example uses ansi C. The main effect of this keyword is to determine how single-floating point valueas are passed to the C function. When:ansi-c is specified single floats are passed through as single-floats, otherwise they are passed through as double floats.


LispWorks Foreign Language Interface - 12 Oct 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker