I've just started learning Lisp and I can't figure out how to compile and link lisp code to an executable.
I'm using clisp
and clisp -c
produces two files:
- .fas
- .lib
What do I do next to get an executable?
I've just started learning Lisp and I can't figure out how to compile and link lisp code to an executable.
I'm using clisp
and clisp -c
produces two files:
What do I do next to get an executable?
I was actually trying to do this today, and I found typing this into the CLisp REPL worked:
(EXT:SAVEINITMEM "executable.exe"
:QUIET t
:INIT-FUNCTION 'main
:EXECUTABLE t
:NORC t)
where main is the name of the function you want to call when the program launches, :QUIET t
suppresses the startup banner, and :EXECUTABLE t
makes a native executable.
It can also be useful to call
(EXT:EXIT)
at the end of your main function in order to stop the user from getting an interactive lisp prompt when the program is done.
EDIT: Reading the documentation, you may also want to add :NORC t
(read link). This suppresses loading the RC file (for example, ~/.clisprc.lisp
).
This is a Lisp FAQ (slightly adapted):
*** How do I make an executable from my programme?
This depends on your implementation; you will need to consult your vendor's documentation.
With ECL and GCL, the standard compilation process will produce a native executable.
With LispWorks, see the Delivery User's Guide section of the documentation.
With Allegro Common Lisp, see the Delivery section of the manual.
etc...
However, the classical way of interacting with Common Lisp programs does not involve standalone executables. Let's consider this during two phases of the development process: programming and delivery.
Programming phase: Common Lisp development has more of an incremental feel than is common in batch-oriented languages, where an edit-compile-link cycle is common. A CL developer will run simple tests and transient interactions with the environment at the REPL (or Read-Eval-Print-Loop, also known as the listener). Source code is saved in files, and the build/load dependencies between source files are recorded in a system-description facility such as ASDF (which plays a similar role to make in edit-compile-link systems). The system-description facility provides commands for building a system (and only recompiling files whose dependencies have changed since the last build), and for loading a system into memory.
Most Common Lisp implementations also provide a "save-world" mechanism that makes it possible to save a snapshot of the current lisp image, in a form which can later be restarted. A Common Lisp environment generally consists of a relatively small executable runtime, and a larger image file that contains the state of the lisp world. A common use of this facility is to dump a customized image containing all the build tools and libraries that are used on a given project, in order to reduce startup time. For instance, this facility is available under the name EXT:SAVE-LISP in CMUCL, SB-EXT:SAVE-LISP-AND-DIE in SBCL, EXT:SAVEINITMEM in CLISP, and CCL:SAVE-APPLICATION in OpenMCL. Most of these implementations can prepend the runtime to the image, thereby making it executable.
Application delivery: rather than generating a single executable file for an application, Lisp developers generally save an image containing their application, and deliver it to clients together with the runtime and possibly a shell-script wrapper that invokes the runtime with the application image. On Windows platforms this can be hidden from the user by using a click-o-matic InstallShield type tool.
Take a look at the the official clisp homepage. There is a FAQ that answers this question.
http://clisp.cons.org/impnotes/faq.html#faq-exec
CLiki has a good answer as well: Creating Executables
For a portable way to do this, I recommend roswell.
For any supported implementation you can create lisp scripts to run the program that can be run in a portable way by ros
which can be used in a hash-bang line similarly to say a python or ruby program.
For SBCL and CCL roswell can also create binary executables with ros dump executable
.
;; I know this is an old question but the Lisp code I'm looking at is 25 years old :-)
I could not get compilation working with clisp on Windows 10. However, it worked for me with gcl
https://www.cs.utexas.edu/users/novak/gclwin.html
;; my lisp file is jugs2.lisp
gcl -compile jugs2.lisp ;; this produces the file jugs2.o if jugs2.lisp file has no errors
;; run gcl with no parameters to launch the lisp interpreter gcl
;; load the .o file
(load "jugs2.o")
;; create an exe
(si:save-system "jugs2")
;; when the exe is run it needs the dll oncrpc.dll ;; this is in the \lib\gcl-2.6.1\unixport folder that gcl.bat creates.
;; when run it shows a lisp environment, call (main) to run the main function (main)