In almost every case, your first gh_
call will be
gh_enter()
never exits, and the user's code should all be in the
main_prog()
function. argc
and argv
will be
passed to main_prog.
gh_enter()
after Guile has been started up.
Please note that gh_enter
does not load `ice-9/boot-9.scm', which
contains much of Guile's basic functionality, including some necessary
parts of Scheme. This is a limitation, and it is only so because the
basic Scheme language functions have not yet been separated from the
higher-level functionality provided by the `ice-9/boot-9.scm' module.
Here is a note from the Guile mailing list describing how to get around
this problem if you want to run some Guile code before you invoke
gh_repl()
. It is a temporary solution, and a better way of
handling the loading of `ice-9/boot-9.scm' will soon be introduced.
The next problem is that boot-9.scm may only be executed once, otherwise you get a stack overflow. When entering the read-eval-print-loop (repl) with gh_repl, guile loads boot-9.scm. Thus, if you did load boot-9.scm yourself and then later enter the repl, guile will abort with a stack overflow. If you look a little into the guile mailing list archives, you can find a temporary solution to the problem which I posted quite some time ago. It's a trivial fix: 1) rename boot-9.scm into boot-9-tail.scm 2) create a new boot-9.scm, which only contains the following code: (if (not (defined? 'provide)) (primitive-load-path "ice-9/boot-9-tail.scm")) With this modification, boot-9.scm can be read several times.
Also note that you can use gh_repl
inside gh_enter
if you
want the program to be controled by a Scheme read--eval--print--loop.
Invoking gh_repl
will load `ice-9/boot-9.scm'.
A convenience routine which enters the Guile interpreter with the standard Guile read--eval--print--loop (REPL) is:
Note that gh_repl
should be used inside gh_enter
,
since any Guile interpreter calls are meaningless unless they happen in
the context of the interpreter.
Also note that when you use gh_repl
, your program will be
controlled by Guile's REPL (which is written in Scheme and has many
useful features). Use straight C code inside gh_enter
if you
want to maintain execution control in your C program.
You will typically use gh_enter
and gh_repl()
when you
want a Guile interpreter enhanced by your own libraries, but otherwise
quite normal. For example, to build a Guile--derived program that
includes some random number routines GSL (GNU Scientific Library),
you would write a C program that looks like this:
#include <guile/gh.h> #include <gsl_ran.h> /* random number suite */ SCM gw_ran_seed(SCM s) { gsl_ran_seed(gh_scm2int(s)); return SCM_UNSPECIFIED; } SCM gw_ran_random() { SCM x; x = gh_ulong2scm(gsl_ran_random()); return x; } SCM gw_ran_uniform() { SCM x; x = gh_double2scm(gsl_ran_uniform()); return x; } SCM gw_ran_max() { return gh_double2scm(gsl_ran_max()); } void init_gsl() { /* random number suite */ gh_new_procedure("gsl-ran-seed", gw_ran_seed, 1, 0, 0); gh_new_procedure("gsl-ran-random", gw_ran_random, 0, 0, 0); gh_new_procedure("gsl-ran-uniform", gw_ran_uniform, 0, 0, 0); gh_new_procedure("gsl-ran-max", gw_ran_max, 0, 0, 0); } void main_prog (int argc, char *argv[]) { init_gsl(); gh_repl(argc, argv); } int main (int argc, char *argv[]) { gh_enter (argc, argv, main_prog); }
Then, supposing the C program is in `guile-gsl.c', you could compile it with gcc -o guile-gsl guile-gsl.c -lguile -lgsl.
The resulting program `guile-gsl' would have new primitive
procedures (gsl-ran-random)
, (gsl-ran-gaussian)
and so
forth.
Go to the first, previous, next, last section, table of contents.