Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lisp as a shebang script vs lisp running in SLIME

I just started with common-lisp, having come from C++ and Python. I'm trying to run a simple SDL program that does nothing other than show an image on-screen. I can get it working from within SLIME. The problem is, it won't work when run from the shell as a script.

My program looks like this:

#!/usr/bin/sbcl --script

(asdf:operate 'asdf:load-op :lispbuilder-sdl)

(defun main ()
  (sdl:with-init ()
    (sdl:window 320 240)
    (sdl:draw-surface (sdl:load-image "image.png"))
    (sdl:update-display)
    (sdl:with-events ()
      (:quit-event () t)
      (:video-expose-event () (sdl:update-display)))))

(main)

When I run this as a script, I get the following error:

mkg@chisel:~/projects/common-lisp/sandbox$ ./hello-world.lisp 
unhandled ASDF:MISSING-COMPONENT in thread #<SB-THREAD:THREAD "initial thread" RUNNING {AA5E849}>:
  component "lispbuilder-sdl" not found

0: (SB-DEBUG::MAP-BACKTRACE #<CLOSURE (LAMBDA #) {AAF1EF5}>)[:EXTERNAL]

(... long backtrace omitted)

Oddly, this program works fine if I do the following. I open the program in Emacs, start SLIME in another window, and in the SLIME window, I enter the first line of the program:

(asdf:operate 'asdf:load-op :lispbuilder-sdl)

Then, in the editor window, I hit C-c C-k (compile/load file). This pops up a window showing image.png, as expected.

Why does this not work when run as a shebang script? How can I fix it?

like image 223
SuperElectric Avatar asked Feb 06 '11 16:02

SuperElectric


1 Answers

As the man page for sbcl says, --script implies --no-sysinit --no-userinit --disable-debugger --end-toplevel-options, which means that initialization files are not read, and so if you set up ASDF registry there it is not set up, and so it cannot find the lispbuilder-sdl system. You need to either set up the registry in the script itself, or save an executable core with the registry already set up and call that instead of the default sbcl. Usually you can also save libraries in the core instead of loading them in the script, but I am not quite sure how that interacts with non-Lisp libraries and resources.

like image 170
Ramarren Avatar answered Nov 16 '22 03:11

Ramarren