24

(David James both wrote the question and an answer. I'll edit it to conform to Stackoverflow standards.)

Using SBCL you can compile Lisp code to machine code.

Like Java, .net, C++ and even C you will need the runtime. So there are two ways to compile Common Lisp code.

First is to make huge binaries which is explained in SBCL documentation. No SBCL needed on target machine.

The other way is a more flexible one, which is to create machine code in a fasl (FASt Load) format. The SBCL runtime is needed on the target machine.

How does the second way work under a Unix-like operating system?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
David James
  • 635
  • 6
  • 16
  • 1
    It's okay to provide answers that you already know. However, please phrase your question as a *question* and provide an answer in the answer section. See http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer-before-asking for more details. – Greg Hewgill Jan 29 '12 at 18:31
  • 5
    I can not provide it as an answer since my reputation is still low. – David James Jan 29 '12 at 18:36
  • 2
    It's nice to see how you start asking about lisp... using parenthesis. – mgarciaisaia Jan 28 '16 at 14:26

2 Answers2

28

(Answer by David James:)

We are going to make two commands in our system: one for batch compiling Lisp code and the other for easily running Lisp code:

Using your favorite editor, open a file called sbcl.compile. The content should be:

    #!/bin/bash
    sbcl --noinform --eval "(compile-file \"$1\")" --eval "(quit)" > /dev/null

Now to compile Lisp files use:

    # sbcl.compile hello.lisp

This will create a hello.fasl file.

Now to easily run these files, we create a new command. Using your favorite editor open a file called sbcl.run. The content should be:

    #!/bin/bash
    sbcl --noinform --load "$1" --quit --end-toplevel-options "$@"

Now you may invoke sbcl.run hello.fasl to run the native code.

    # sbcl.run hello.fasl

Details are described in the SBCL manual: Starting SBCL

kfsone
  • 23,617
  • 2
  • 42
  • 74
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    This doesn't work for me. The first script `sbcl.compile', produces a fasl, but the second script gives `sh sbcl.run bar.fasl fatal error before reaching READ-EVAL-PRINT loop: bad toplevel option: "bar.fasl"`. – Faheem Mitha May 10 '12 at 22:00
  • 2
    For dev purposes, you can just `sbcl --script source.lisp`. – mgarciaisaia Jan 28 '16 at 14:29
  • The manual says that `--script` at top level implies `--no-userinit --no-sysinit --disable-debugger --end-toplevel-options`, so just doing `sbcl --script hello.fasl` seems to be much easier and more appropriate. – Renato Sep 22 '21 at 20:59
4

Another option is to add to the runtime all packages/functions/frameworks that you commonly use, and then save this state as a new core file, and use this as your default core while continuing development. I usually find fasls more trouble than they are worth, especially since lisp has the ability to save state in a VM-style core file. I just incrementally update the core as development moves along. And rebuild/update the core using GNU Make.

Clayton Stanley
  • 7,513
  • 9
  • 32
  • 46