SBCLで実行可能なバイナリを作る

Google先生で検索しまくって見付けた. でも, はっきり言って実用性ないと思う.
ネタにしかならない.
他に良い方法を知ってる人が居たら是非教えてください.



とりあえず, 端末エミュレータ上からSBCLを起動する.
ちなみに, rsbclってなってんのは, .zshrcで「alias rsbcl="rlwrap sbcl"」としているから.

$ rsbcl
This is SBCL 1.0, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.

それで, 「5+2」の結果を表示するだけのつまらない関数を実行してみる.

* (defun add-5-2 () (print (+ 5 2)))

ADD-5-2
* (add-5-2)

7
7

こいつを実行可能なバイナリに落としてみる.


「(sb-ext:save-lisp-and-die "test-bin" :executable t :toplevel 'add-5-2)」とする.
「test-bin」は, 作成されるファイルの名前.
最後の「add-5-2」は, メインとなる関数.

* (sb-ext:save-lisp-and-die "test-bin" :executable t :toplevel 'add-5-2)
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into /home/khiker/program/lisp/sbcl/test-bin:
writing 1912 bytes from the read-only space at 0x01000000
writing 1936 bytes from the static space at 0x05000000
writing 23662592 bytes from the dynamic space at 0x09000000
done]

こいつを実行してみる.

$ ./test-bin
This is SBCL 1.0, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.

7 %

最後改行してないから, 「%」が表示されてるけど, まあそれは御愛嬌.



で, 何が使いものにならないかと言うと, ファイルの大きさ.
何というかでかい.

$ du -h test-bin
24M     test-bin

見てのとおり, これだけで24Mがもある.
あくまで予想だけど, こいつをまんま含めちゃってるからじゃねーかと予想.

$ du -h /usr/local/lib/sbcl/sbcl.core
23M     /usr/local/lib/sbcl/sbcl.core

ちなみに, こいつ(test-bin)をstripしたら, ファイルの大きさは139kまで減るけど, 単純にSBCLが起動するのと同じになる.


たぶん, 「sb-ext:save-lisp-and-die」は, まったく別の用途で使うためにある関数なんだと思う.
どういう用途なのかは, 自分の実力じゃ全然分かんないけど.
まあLispはじめて2週目ぐらいで, sbclに至っては, 1週間たってないしw



ちなみに, Emacs + SLIME上でやろうとしたら, 何故かできなかった. save-lisp-and-dieが実行できない.
また, ファイルに関数を書いて, それを読み込ませて, バイナリを作ってみたら,
そのできたバイナリは, 実行できて結果も表示されたけど, デバッガが起動して終了させらんなくなった.
端末エミュレータごと落として, 無理矢理終わらせたけど, 何だったんだろう?