buildcommon-lispsbcl

Compiling Common Lisp to an executable


I recently started learning Common Lisp using SBCL. How can I compile my Lisp programs into a Windows binary?


Solution

  • Making hello.exe:

    * (defun main () (print "hello"))
    
    MAIN
    * (sb-ext:save-lisp-and-die "hello.exe" :toplevel #'main :executable t)
    [undoing binding stack and other enclosing state... done]
    [saving current Lisp image into hello.exe:
    writing 3160 bytes from the read-only space at 0x22000000
    writing 2592 bytes from the static space at 0x22100000
    writing 30134272 bytes from the dynamic space at 0x22300000
    done]
    > dir hello.exe
    31,457,304 hello.exe
    > hello.exe
    
    "hello"
    

    31 MB executable file!


    Using ASDF's UIOP:

    https://quickref.common-lisp.net/uiop.html#index-dump_002dimage

    This also works in other Lisps, including SBCL:

    * (require "asdf")
    ("ASDF" "asdf" "UIOP" "uiop")
    * (defun main () (print "hello"))
    MAIN
    * (setq uiop:*image-entry-point* #'main)
    #<FUNCTION MAIN>
    * (uiop:dump-image "hello.exe" :executable t)
    [undoing binding stack and other enclosing state... done]
    [performing final GC... done]
    [defragmenting immobile space... (inst,fdefn,code,sym)=828+16731+18346+26762... done]
    [saving current Lisp image into hello.exe:
    writing 1936 bytes from the static space at 0000000020020000
    writing 18743296 bytes from the dynamic space at 0000001000000000
    writing 6786336 bytes from the read-only space at 0000000fff980000
    writing 1937408 bytes from the fixedobj space at 0000000020120000
    writing 11816960 bytes from the text space at 0000000021a20000
    done]
    
    > dir hello.exe
    41,817,704 hello.exe