Next: , Previous: Linking Guile into Programs, Up: Whirlwind Tour

2.3.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 extensions, 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_make_real (j0 (scm_num2dbl (x, "j0")));
     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 -shared -o -fPIC bessel.c

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

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

     $ guile
     guile> (load-extension "./libguile-bessel" "init_bessel")
     guile> (j0 2)