Scheme 48 Manual | Contents | In Chapter: Mixing Scheme 48 and C
Previous: Shared bindings | Next: Adding external modules to the Makefile

Calling C functions from Scheme

There are three different ways to call C functions from Scheme, depending on how the C function was obtained.

Each of these applies its first argument, a C function, to the rest of the arguments. For call-imported-binding the function argument must be an imported binding. For call-external the function argument must be an external bound in the current process (see the section on Dynamic Loading). For call-external-value value must be a byte vector whose contents is a pointer to a C function and name should be a string naming the function. The name argument is used only for printing error messages.

For all of these, the C function is passed the argi values and the value returned is that returned by C procedure. No automatic representation conversion occurs for either arguments or return values. Up to twelve arguments may be passed. There is no method supplied for returning multiple values to Scheme from C (or vice versa) (mainly because C does not have multiple return values).

Keyboard interrupts that occur during a call to a C function are ignored until the function returns to Scheme (this is clearly a problem; we are working on a solution).

These macros simplify importing functions from C. They define name to be a function with the given formals that applies those formals to the corresponding C binding. C-name, if supplied, should be a string. These expand into
(define temp (lookup-imported-binding c-name))
(define name
  (lambda (formal ...)
    (external-apply temp formal ...)))

If c-name is not supplied, it is derived from name by converting all letters to lowercase and replacing `-' with `_'.

Previous: Shared bindings | Next: Adding external modules to the Makefile