Scheme 48 Manual -- Dynamic loading

Scheme 48 Manual | Contents | In Chapter: Mixing Scheme 48 and C
Previous: Dynamic loading | Next: Dynamic loading

## 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 string)`

`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 `foo.so` that can be loaded dynamically.

>     % gcc -c -o foo.o foo.c
>     % ld -shared -o foo.so foo.o

- `(get-external string) -> external`
- `(external? x) -> boolean`
- `(external-name external) -> string`
- `(external-value external) -> byte-vector`

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 external) -> boolean`
- `(lookup-all-externals) -> boolean`

`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.

- `(call-external external arg0 ...) -> value`

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: Dynamic loading | Next: Dynamic loading