This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Clisp 2.49: compiling source code

3 posts · 3357 views

Hi, everybody!

Working with C++ for sometime, I got used to IDEs etc. But having recently started studying Common Lisp, to my shame, I can't understand how to compile and "run" source code files with clisp. Assuming I have already downloaded clisp 2.49 and i have at at C:\CLisp source code file hyp.lsp with such code:
;;Printing
(princ "Hello, world!: ")
(bye)
what should I do to have text "Hello,world!" appeared on the console?

thanks for any answers

Re: Clisp 2.49: compiling source code

Are you reading a book like Practical Common Lisp? It explains how CL is supposed to be used.

In general, compilation means translating a program from one form to another, and doesn't necessarily involve external files. Most compiling Common Lisp implementations store the compiled form in internal environment. CLISP only had compiler since fairly recently, and I am not sure if it is operational under Windows. CCL is another CL implementation which I hear works well under Windows, and has a pretty good compiler.

In any case, usually you wouldn't "compile" a CL program to an external file, but rather run it from within CL environment, as described in Practical Common Lisp. Creating executables is fairly rare and mostly used for deployment. In most implementations you can also run CL source files as scripts, but I don't know how that works on Windows.

Mostly open source CL is fairly weakly supported on Windows. If you are willing to pay for the operating system you should be willing to pay for a commercial CL system like Lispworks or Allegro CL, which come with their own IDEs and deployment facilities.

Re: Clisp 2.49: compiling source code

ferbolg wrote:What should I do to have text "Hello,world!" appeared on the console?
First of all: if you use 'princ' you should make sure that the text is really printed to the screen:
;;Printing
(princ "Hello, world!: ")
(terpri)  ; terminate printing = flush the buffer
(bye)     ; quit CLISP
Now you can run the plain text file like this:
C:\CLisp> clisp -q -norc hyp.lsp
Hello, world!:
C:\CLisp>
-q = do not show any welcome messages
-norc = do not read any initialisation files

You can speed up the read-time [but in CLISP this doesn't speed-up the execution-time], by byte-compiling the "hyp.lsp" file.

From within CLISP you can change the current working directory by writing:
[1]> (cd "C:\\CLisp")
#p"C:\\CLisp"
If you are in the "C:\CLisp" directory, write:
[2]> (compile-file "hyp.lsp")
The screen output should look like:
;; Compiling file hyp.lsp ...
;; Wrote file hyp.fas
0 Errors, 0 Warnings
#P"hyp.fas" ;
NIL ;
NIL
This should produce a file "hyp.fas", that you can run by writing:
C:\CLisp> clisp -q -norc hyp.fas
Hello, world!:
C:\CLisp>
If you want to run it as a stand-alone executable, you should do the following:

First define a function:
(defun hello-world ()
  (princ "Hello, world!: ")
  (terpri)
  (bye))
From within CLISP this looks like::
[3]> (defun hello-world ()
       (princ "Hello, world!: ")
       (terpri)
       (bye))
HELLO-WORLD
Warning: do NOT try to run the 'hello-world' function by typing (hello-world) at the CLISP prompt, because of the (bye) line CLISP will terminate instantly!

Now, after defining the 'hello-world' function at the CLISP prompt, dump a Lisp image and save it in a "myprog" executable:
[4]> (saveinitmem "myprog" :quiet t :norc t :executable t :init-function 'hello-world)
:quiet t = do not show any welcome messages
:norc t = do not read any initialisation files
:executable t = write a stand-alone executable
:init-function 'hello-world = after starting the executable, call the 'hello-world' function

The screen output should look like:
;; Wrote the memory image into myprog (8,489,838 bytes)
Bytes permanently allocated:             94,080
Bytes currently in use:               2,131,408
Bytes available until next GC:          530,852
2131408 ;
530852 ;
94080 ;
1 ;
35816 ;
32002
I haven't worked on Windows for a very long time, but according to the CLISP docs, on Windows "myprog" should be automatically extended to "myprog.exe". If it doesn't, just simply type "myprog.exe" instead of "myprog" after 'saveinitmem' above.

The executable was saved into the current CLISP working directory. In case of doubt type:
[5]> (cd)
#p"name-of-current-working-directory"
Now you should be able to run the stand-alone executable from the Windows commandline by typing:
C:\CLisp> myprog
Hello, world!:
C:\CLisp>
Plase note that the executable will contain the full Common Lisp runtime system, so this will probably be the biggest "Hello World" executable ever. Common Lisp was designed to solve "big" programming tasks, it is not particularly well suited for writing mini-executables.

For the full details of 'saveinitmem' see http://clisp.cons.org/impnotes.html#image