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

Comments are closed.

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.

Mentions

  • My Scheme Stuff: Category: Scheme: My blog posts on Scheme. scheme-test-unit: My unit test runner for SRFI-64. Basic Games in Scheme: Classic BASIC games &…