Next: , Previous: Running Guile Scripts, Up: Whirlwind Tour


2.3.3 Linking Guile into Programs

The Guile interpreter is available as an object library, to be linked into applications using Scheme as a configuration or extension language.

Here is simple-guile.c, source code for a program that will produce a complete Guile interpreter. In addition to all usual functions provided by Guile, it will also offer the function my-hostname.

     #include <stdlib.h>
     #include <libguile.h>
     
     static SCM
     my_hostname (void)
     {
       return scm_str2string (getenv ("HOSTNAME"));
     }
     
     static void
     inner_main (void *data, int argc, char **argv)
     {
       scm_c_define_gsubr ("my-hostname", 0, 0, 0, my_hostname);
       scm_shell (argc, argv);
     }
     
     int
     main (int argc, char **argv)
     {
       scm_boot_guile (argc, argv, inner_main, 0);
       return 0; /* never reached */
     }

When Guile is correctly installed on your system, the above program can be compiled and linked like this:

     $ gcc -o simple-guile simple-guile.c -lguile

When it is run, it behaves just like the guile program except that you can also call the new my-hostname function.

     $ ./simple-guile
     guile> (+ 1 2 3)
     6
     guile> (my-hostname)
     "burns"