To compile a Scheme program (assuming a UNIX-like environment) we perform the following steps:
Consider this Scheme source file, named foo.scm
;;; foo.scm (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))) ) ) (write (fac 10)) (newline)
Compile the file foo.scm
% chicken foo.scm
Compile the generated C file foo.c
% gcc foo.c -o foo `chicken-config -cflags -libs`
Start the compiled program
% foo
3628800
If multiple bodies of Scheme code are to be combined into a single executable, then we have to compile each file and link the resulting object files together with the runtime system:
Consider these two Scheme source files, named foo.scm and bar.scm
;;; foo.scm (declare (uses bar)) (write (fac 10)) (newline) ;;; bar.scm (declare (unit bar)) (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))) ) )
Compile the files foo.scm and bar.scm
% chicken foo.scm % chicken bar.scm
Compile the generated C files foo.c and bar.c
% gcc -c foo.c `chicken-config -cflags` % gcc -c bar.c `chicken-config -cflags`
Link the object files foo.o and bar.o
% gcc foo.o bar.o -o foo `chicken-config -libs`
Start the compiled program
% foo 3628800
The declarations specify which of the compiled files is the main module, and which is the library module. An executable can only have one main module, since a program has only a single entry-point. In this case foo.scm is the main module, because it doesn't have a unit declaration.
Extensions to the basic Chicken runtime libraries are available in a separate utility library (libsrfi-chicken.[a|so] and libstuffed-chicken.[a|so] on UNIX-like platforms, libsrfi-chicken.lib and libstuffed-chicken.lib on Windows systems). Whenever you use one or more of the units extras, format, srfi-1, srfi-4, srfi-13, srfi-14, srfi-18, srfi-25, srfi-37, posix, script-utils, lolevel, tinyclos or regex, then you should add these library to the command line of the C compiler or linker (this is equivalent to adding the -extra-libs option to chicken-config):
;;; foo.scm (declare (uses posix format)) (format #t "Anno Domini ~@R~%" (+ 1900 (vector-ref (seconds->local-time (current-seconds)) 5))) % chicken foo.scm % gcc foo.c `chicken-config -cflags -extra-libs -libs` -o foo % foo Anno Domini MMIII