## 3.5 Distributing compiled C files

It is relatively easy to create distributions of Scheme projects that have been compiled to C. The runtime system of Chicken consists of only two handcoded C files (`runtime.c` and `chicken.h`), plus the file `chicken-config.h`, which is generated by the build process. All other modules of the runtime system and the extension libraries are just compiled Scheme code.

An example:

- Consider these two source files:

      ;;; foo.scm

      (declare (uses bar))

      (say-hello)

  and

      ;;; bar.scm

      (declare (unit bar) (uses posix))

      (define (say-hello)
        (print "Hello, " (nth-value 0 (user-information (current-user-id)))) )

- Compiling them yields two C files:

      % chicken foo.scm -quiet
      % chicken bar.scm -quiet

- The makefile for generating the executable `foo` would look like this:

      CC = gcc
      CFLAGS = -I. -O3 -fomit-frame-pointer -fstrict-aliasing

      foo : foo.o bar.o libchicken.a libsrfi-chicken.a libstuffed-chicken.a
              gcc -s -L. foo.o bar.o -o $@ -lchicken -lsrfi-chicken -lstuffed-chicken
      foo.o : foo.c chicken.h chicken-config.h
      bar.o : bar.c chicken.h chicken-config.h

      libchicken.a : runtime.o library.o eval.o syntax-case.o
              ar rus $@ $^
      libsrfi-chicken.a : srfi-1.o srfi-4.o srfi-13.o srfi-14.o scheduler.o srfi-18.o srfi-25.o srfi-37.o
              ar rus $@ $^
      libstuffed-chicken.a : extras.o format.o match-support.o lolevel.o regex.o posix.o script-utils.o tcp.o
              ar rus $@ $^
      runtime.o : runtime.c chicken.h chicken-config.h
      eval.o : eval.c chicken.h chicken-config.h
      library.o : library.c chicken.h chicken-config.h
      extras.o : extras.c chicken.h chicken-config.h
      srfi-1.o : srfi-1.c chicken.h chicken-config.h
      srfi-4.o : srfi-4.c chicken.h chicken-config.h
      match-support.o : match-support.c chicken.h chicken-config.h
      syntax-case.o : syntax-case.c chicken.h chicken-config.h
      srfi-18.o : srfi-18.c chicken.h chicken-config.h
      scheduler.o : scheduler.c chicken.h chicken-config.h
      format.o : format.c chicken.h chicken-config.h
      tinyclos.o : tinyclos.c chicken.h chicken-config.h
      srfi-13.o : srfi-13.c chicken.h chicken-config.h
      srfi-14.o : srfi-14.c chicken.h chicken-config.h
      srfi-25.o : srfi-25.c chicken.h chicken-config.h
      srfi-37.o : srfi-37.c chicken.h chicken-config.h
      lolevel.o : lolevel.c chicken.h chicken-config.h
      regex.o : regex.c chicken.h chicken-config.h
      posix.o : posix.c chicken.h chicken-config.h
      script-utils.o : script-utils.c chicken.h chicken-config.h
      tcp.o : tcp.c chicken.h chicken-config.h

- If Chicken was properly installed, then the C files needed can be found in the directory `$(CHICKEN_HOME)/src` (or just take them from the original tar archive). Don't forget `chicken.h` / `chicken-config.h`.

- Some of the libraries created during the build process might not be needed. In this case it is safe to remove the associated rules from the makefile. Most of the time not all library units are needed for a given application. For the example above, a minimal makefile would look like this:

      CC = gcc
      CFLAGS = -I. -O3 -fomit-frame-pointer -fstrict-aliasing

      foo : foo.o bar.o libchicken0.a libstuffed-chicken0.a
              gcc -s -L. foo.o bar.o -o $@ -lchicken0 -lstuffed-chicken0
      foo.o : foo.c chicken.h chicken-config.h
      bar.o : bar.c chicken.h chicken-config.h

      libchicken0.a : runtime.o library.o eval.o
              ar rus $@ $^
      libstuffed-chicken0.a : posix.o regex.o extras.o script-utils.o
              ar rus $@ $^
      runtime.o : runtime.c chicken.h chicken-config.h
      eval.o : eval.c chicken.h chicken-config.h
      library.o : library.c chicken.h chicken-config.h
      posix.o : posix.c chicken.h chicken-config.h
      regex.o : regex.c chicken.h chicken-config.h
      extras.o : extras.c chicken.h chicken-config.h
      script-utils.o : script-utils.c chicken.h chicken-config.h

- Most library units can be used independently. Here is a table that shows library units that depend on other library units:

  format | extras
  srfi-13 | srfi-14
  lolevel | extras
  tinyclos | extras
  posix | extras regex script-utils
  script-utils | extras regex
  srfi-18 | scheduler
  tcp | extras scheduler

  In the example above `bar` uses `posix`. The units `library` and `eval` are used by `foo` (these are used by default). The file `runtime.c` is needed in any case.