( How to get started with libguile

Info Catalog ( What is libguile ( Guile in a Library ( More interesting programming with libguile
 4.3 How to get started with libguile
 Here is an elementary first program, `learn0', to get going with
 libguile.  The program (which uses Scheme as a master world) is in a
 single source file, `learn0.c':
      /* test the new libgh.a (Guile High-level library) with a trivial
         program */
      #include <stdio.h>
      #include <guile/gh.h>
      void main_prog(int argc, char *argv[]);
      main(int argc, char *argv[])
        gh_enter(argc, argv, main_prog);
      void main_prog(int argc, char *argv[])
        int done;
        char input_str[200];
        gh_eval_str("(display \"hello Guile\")");
        /* for fun, evaluate some simple Scheme expressions here */
        gh_eval_str("(define (square x) (* x x))");
        gh_eval_str("(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))");
        gh_eval_str("(square 9)");
        /* now sit in a Scheme eval loop: I input the expressions, have
           Guile evaluate them, and then get another expression. */
        done = 0;
        fputs("learn0> ", stdout);
        while (fgets(input_str, 199, stdin) != NULL) {
          fputs("\nlearn0> ", stdout);
   If you name this program `learn0.c', it can now be compiled with:
      gcc -g -c learn0.c -o learn0.o
      gcc -o learn0 learn0.o -lguile -lm
   The program is simple: it creates a Scheme interpreter, passes a
 couple of strings to it that define new Scheme functions `square' and
 `factorial', and then a couple of strings that invoke those functions.
   It then goes into a read-eval-print-loop (REPL), so you could type
 one-line Scheme expressions to it and have them evaluated.  For example:
      <shell-prompt> ./learn0
      hello Guile
      learn0> (display (sin 1.3))
      learn0> (display (fact 10))
      learn0> (quit)
   You should notice the key steps involved in this `learn0' program:
   1. `#include <guile/gh.h>'
   2. You need to invoke the initialization routine `gh_enter()'.  This
      starts up a Scheme interpreter, handling many
      implementation-specific details.
   3. Your main() function should be almost empty: the real main program
      goes in a separate function main_prog() which is passed to
      gh_enter().  This rather arcane convention is due to the way
      Guile's garbage collector works: the whole program has to run in
      the dynamic context of `gh_enter()'.
   4. You pass strings to the Scheme interpreter with the `gh_eval_str()'
   5. You link your program with `-lguile'.
Info Catalog ( What is libguile ( Guile in a Library ( More interesting programming with libguile
automatically generated byinfo2html