Previous: Build Options, Up: Building SCM


2.3.3 Compiling and Linking Custom Files

A correspondent asks:

How can we link in our own c files to the SCM interpreter so that we can add our own functionality? (e.g. we have a bunch of tcp functions we want access to). Would this involve changing build.scm or the Makefile or both?

(see Changing Scm has instructions describing the C code format). Suppose a C file foo.c has functions you wish to add to SCM. To compile and link your file at compile time, use the ‘-c’ and ‘-i’ options to build:

     bash$ ./build -c foo.c -i init_foo
     -|
     #! /bin/sh
     rm -f scmflags.h
     echo '#define IMPLINIT "/home/jaffer/scm/Init5e7.scm"'>>scmflags.h
     echo '#define COMPILED_INITS init_foo();'>>scmflags.h
     echo '#define BIGNUMS'>>scmflags.h
     echo '#define FLOATS'>>scmflags.h
     echo '#define ARRAYS'>>scmflags.h
     gcc -O2 -c continue.c scm.c findexec.c script.c time.c repl.c scl.c \
             eval.c sys.c subr.c unif.c rope.c foo.c
     gcc -rdynamic -o scm continue.o scm.o findexec.o script.o time.o \
             repl.o scl.o eval.o sys.o subr.o unif.o rope.o foo.o -lm -lc

To make a dynamically loadable object file use the -t dll option:

     bash$ ./build -t dll -c foo.c
     -|
     #! /bin/sh
     rm -f scmflags.h
     echo '#define IMPLINIT "/home/jaffer/scm/Init5e7.scm"'>>scmflags.h
     echo '#define BIGNUMS'>>scmflags.h
     echo '#define FLOATS'>>scmflags.h
     echo '#define ARRAYS'>>scmflags.h
     echo '#define DLL'>>scmflags.h
     gcc -O2 -fpic -c foo.c
     gcc -shared -o foo.so foo.o -lm -lc

Once foo.c compiles correctly (and your SCM build supports dynamic-loading), you can load the compiled file with the Scheme command (load "./foo.so"). See Configure Module Catalog for how to add a compiled dll file to SLIB's catalog.