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

Dynamic loading

External code can be loaded into a running Scheme 48 process and C object-file bindings can be dereferenced at runtime and their values called (although not all versions of Unix support all of this). The required Scheme functions are in the structure dynamic-externals.

Dynamic-load loads the named file into the current process, raising an exception if the file cannot be found or if dynamic loading is not supported by the operating system. The file must have been compiled and linked appropriately. For Linux, the following commands compile foo.c into a file that can be loaded dynamically.
% gcc -c -o foo.o foo.c
% ld -shared -o foo.o
These functions give access to values bound in the current process, and are used for retrieving values from dynamically-loaded files. Get-external returns an external object that contains the value of name, raising an exception if there is no such value in the current process. External? is the predicate for externals, and external-name and external-value return the name and value of an external. The value is returned as byte vector of length four (on 32-bit architectures). The value is that which was extant when get-external was called. The following two functions can be used to update the values of externals. Lookup-external updates the value of external by looking up its name in the current process, returning #t if the name is bound and #f if it is not. Lookup-all-externals calls lookup-external on all extant externals, returning #f any are unbound. An external whose value is a C procedure can be called using call-external. See the section on calling C functions from Scheme for more information.

In some versions of Unix retrieving a value from the current process may require a non-trivial amount of computation. We recommend that a dynamically-loaded file contain a single initialization procedure that creates shared bindings for the values exported by the file.

Previous: Adding external modules to the Makefile | Next: Compatibility