Clisp 2.49: compiling source code

Discussion of Common Lisp
Post Reply
ferbolg
Posts: 1
Joined: Wed Sep 01, 2010 9:45 pm

Clisp 2.49: compiling source code

Post by ferbolg » Wed Sep 01, 2010 10:15 pm

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:

Code: Select all

;;Printing
(princ "Hello, world!: ")
(bye)
what should I do to have text "Hello,world!" appeared on the console?

thanks for any answers

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Clisp 2.49: compiling source code

Post by ramarren » Wed Sep 01, 2010 11:03 pm

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.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Clisp 2.49: compiling source code

Post by edgar-rft » Thu Sep 02, 2010 2:24 am

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:

Code: Select all

;;Printing
(princ "Hello, world!: ")
(terpri)  ; terminate printing = flush the buffer
(bye)     ; quit CLISP
Now you can run the plain text file like this:

Code: Select all

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:

Code: Select all

[1]> (cd "C:\\CLisp")
#p"C:\\CLisp"
If you are in the "C:\CLisp" directory, write:

Code: Select all

[2]> (compile-file "hyp.lsp")
The screen output should look like:

Code: Select all

;; 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:

Code: Select all

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:

Code: Select all

(defun hello-world ()
  (princ "Hello, world!: ")
  (terpri)
  (bye))
From within CLISP this looks like::

Code: Select all

[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:

Code: Select all

[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:

Code: Select all

;; 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:

Code: Select all

[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:

Code: Select all

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

Post Reply