[previous] [up] [next]     [index]
Next: Embedding MzScheme into a Up: Overview Previous: Overview

Writing MzScheme Extensions

To write a C/C++-based extension for MzScheme, follow these steps:

IMPORTANT: Scheme values are garbage collected using a conservative garbage collector, so pointers to MzScheme objects can be kept in registers, stack variables, or structures allocated with scheme_malloc. However, static variables that contain pointers to collectable memory must be registered using scheme_register_extension_global (see section 2.2).

As an example, the following C code defines an extension that returns "hello world" when it is loaded:

 #include "escheme.h"
 Scheme_Object *scheme_initialize(Scheme_Env *env) {
   return scheme_make_string("hello world");
 }
 Scheme_Object *scheme_reload(Scheme_Env *env) {
   return scheme_initialize(env); /* Nothing special for reload */
 }

Assuming that this code is in the file hw.c, the extension is compiled under Unix with the following two commands:

  mzc --cc hw.c
  mzc --ld hw.so hw.o 
(Note that the --cc and --ld flags are each prefixed by two dashes, not one.)

The plt/collects/mzscheme/examples directory in the PLT distribution contains additional examples.


[previous] [up] [next]     [index]
Next: Embedding MzScheme into a Up: Overview Previous: Overview

PLT