Node:Intro to Modules and Extensions, Previous:Intro to Writing New Modules, Up:Guile Modules

5.5.3 Intro to Modules and Extensions

In addition to Scheme code you can also put things that are defined in C into a module.

You do this by writing a small Scheme file that defines the module. That Scheme file in turn invokes load-extension to make the features defined in C available. This works since all definitions made by scm_c_define_gsubr etc. go into the current module and define-module causes the newly defined module to be current while the code that follows it is executed.

Suppose we want to put the Bessel function j0 from the example extension into a module called (math bessel). We would have to write a Scheme file with this contents

(define-module (math bessel))

(export j0)

(load-extension "libguile-bessel" "init_bessel")

This file should of course be saved in the right place for autoloading, for example as /usr/local/share/guile/math/bessel.scm.

When init_bessel is called, the new (math bessel) module is the current one. Thus, the call to scm_c_define_gsubr will put the new definition for j0 into it, just as we want it.

The definitions made in the C code are not automatically exported from a module. You need to explicitly list the ones you want to export in export statements or with the :export option of define-module.

There is also a way to manipulate the module system from C but only Scheme files can be autoloaded. Thus, we recommend that you define your modules in Scheme.