Warning: This is the manual of the legacy Guile 2.0 series. You may want to read the manual of the current stable series instead.

Next: , Previous: , Up: Hello Guile!   [Contents][Index]

2.4 Writing Guile Extensions

You can link Guile into your program and make Scheme available to the users of your program. You can also link your library into Guile and make its functionality available to all users of Guile.

A library that is linked into Guile is called an extension, but it really just is an ordinary object library.

The following example shows how to write a simple extension for Guile that makes the j0 function available to Scheme code.

#include <math.h>
#include <libguile.h>

j0_wrapper (SCM x)
  return scm_from_double (j0 (scm_to_double (x)));

init_bessel ()
  scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);

This C source file needs to be compiled into a shared library. Here is how to do it on GNU/Linux:

gcc `pkg-config --cflags guile-2.0` \
  -shared -o libguile-bessel.so -fPIC bessel.c

For creating shared libraries portably, we recommend the use of GNU Libtool (see Introduction in GNU Libtool).

A shared library can be loaded into a running Guile process with the function load-extension. The j0 is then immediately available:

$ guile
scheme@(guile-user)> (load-extension "./libguile-bessel" "init_bessel")
scheme@(guile-user)> (j0 2)
$1 = 0.223890779141236

For more on how to install your extension, see Installing Site Packages.