Chicken Scheme modules

worker.scm

(declare (unit worker)) ;; required for static build, remove in dynamic build
(module worker
    (make-worker)

(import scheme)

(define (make-worker n)
    (lambda ()  (set! n (+ n 1))  n) )

) ;; end module worker

main.scm

#!/usr/bin/env csi -s

(import scheme
    (chicken load)
    (chicken process-context)
)

;; repeat for each library
(cond-expand
    (compiling (declare (uses worker)))  ;; required for static build, remove for dynamic build
    (else (load-relative "worker.scm")) )
(import worker)

(define (main argv)
    (let ( (w (make-worker 0)) )
        (print "worker: " (w))
        (print "worker: " (w))
        (print "worker: " (w)) ) )

(main (command-line-arguments)) ;; csi -ss will call main, but no such convenience exists in csc

build.sh

#!/bin/sh

buildtype=$1

if [[ $buildtype == static ]]; then
    csc -c -j worker worker.scm -o worker.o || exit $?
    csc -c main.scm -o main.o || exit $?
    csc main.o worker.o -o main || exit $?  # add -static for a binary with no Chicken dependencies
elif [[ $buildtype == dynamic ]]; then
    csc -j worker worker.scm -o worker.so || exit $?
    csc main.scm -o main || exit $?
else
    echo "Usage: ./build.sh [dynamic|static]"
    exit 1
fi

rm -f *.o *.import.scm *.link

run as script

% ./main.scm

build & run as binary

% ./build.sh static
% ./main