Building Common Lisp Executables
source link: https://susam.in/maze/building-common-lisp-executables.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Building Common Lisp Executables
Since Common Lisp is a language standard (not an implementation) it is hard to provide a single set of instructions or guidelines that would work for all implementations. There are various implementations of Common Lisp that target native machine code, C code, bytecode, JVM, etc. So the build instructions, project structure, etc. depend on the target.
Using SBCL
Here is a minimal example that builds a Lisp program into a binary executable with SBCL:
(defun main ()
(format t "hello, world~%"))
(sb-ext:save-lisp-and-die "hello" :executable t :toplevel #'main)
The SBCL-specific save-lisp-and-die
function saves the
Lisp process as a core image. The :executable t
keyword
argument includes the SBCL runtime in the image to ensure that the
image is a standalone executable. This is why the executable for
even a simple hello-world program tends to be quite large (30 MB to
50 MB)! The :toplevel
argument specifies the function
to run when the core file is run.
Here are some example commands to get you started:
$ cat hello.lisp (defun main () (format t "hello, world~%")) (sb-ext:save-lisp-and-die "hello" :executable t :toplevel #'main) $ sbcl --load hello.lisp $ ./hello hello, world
Moving Unportable Code to Command Line Argument
If you would rather not have SBCL specific code in the Lisp source
code file, then you could move
the sb-ext:save-lisp-and-die
call out of your source
file to the SBCL command invocation. The source code now looks like
this:
(defun main () (format t "hello, world~%"))
The shell commands now look like this:
$ cat hello.lisp (defun main () (format t "hello, world~%")) $ sbcl --load hello.lisp --eval "(sb-ext:save-lisp-and-die \"hello\" :executable t :toplevel #'main)" $ ./hello hello, world
Using Buildapp
By the way, there is also Buildapp that provides a layer of abstraction for building executables from Lisp programs. It works with SBCL and CCL. It requires the toplevel function to be called with an argument though. Therefore the source code needs to be modified to the following:
(defun main (argv)
(declare (ignore argv))
(format t "hello, world~%"))
Then Buildapp can be invoked like this:
$ cat hello.lisp (defun main (argv) (declare (ignore argv)) (format t "hello, world~%")) $ buildapp --load hello.lisp --entry main --output hello ;; loading file #P"/Users/susam/hello.lisp" $ ./hello hello, world
© 2001–2021 Susam Pal
Recommend
-
39
I’ve gotten a bunch of emails asking for advice on how to learn Common Lisp in the present day. I decided to write down all the advice I’ve been giving through email and social media posts in the hopes that someone might...
-
44
Benjamin Slade I was a long-time user of Conkeror , a highly-extensible browser with an Emacs ethos. It
-
62
Numcl This is a Numpy clone in Common Lisp. At the moment the library is written in pure Common Lisp, focusing more on correctness and usefullness, not speed. Track the progress at
-
38
Common Lisp has a rich set of numerical types, including integer, rational, floating point, and complex. Some sources:
-
37
Introduction This is an opinionated guide to writing good, maintainable Common Lisp code. This page is largely based on Google’s Common Lisp S...
-
41
I find myself at a loss of what to write lately and currently, as none of my newer work is currently in a finished or otherwise presentable state; I believe I'll make this month one of rebuttals and of articles that are in...
-
41
Is it possible that each code fragment could be runnable and editable, right in the browser? One way to get there is to create a docker container with the book, the code, a Common Lisp compiler, and an IDE.
-
27
ANSI Common Lisp combines an introduction to Lisp programming, and a convenient, up-to-date reference manual for ANSI Common Lisp. Beginners will find that its careful explanations and interesting exampl...
-
22
I have been reading Practical Common Lisp by Peter Seibel over the weekend, which is an excellent introduction to Common Lisp, showcasing its power by writin...
-
5
Building SBCL - Steel Bank Common Lisp on Windows Posted on August 20, 2019 by Paul In this article, I will show you how to build the latest stable version of SBCL - Steel Bank Comm...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK