You are looking at historical revision 6648 of this page. It may differ significantly from its current revision.

Reducing the size of a compiled executable

Generating the smallest possible, self-contained executable

Here is a small demonstration for creating the smallest possible (while still self-contained) executable. A UNIX-like environment is assumed.

A simple hello-world program:

(display "Hello, world!\n")

You need the files library.c, runtime.c and chicken.h from the Chicken source distribution.

First we generate hello.c from hello.scm:

$ chicken hello.scm -explicit-use -uses library

The options -explicit-use -uses library tell the compiler not to link in the evaluator, we just want the basic Scheme library (and the runtime system).

Now we compile hello.c, the runtime system in runtime.c and the core library library.c into a single executable:

$ gcc hello.c runtime.c library.c -Os -fomit-frame-pointer -fno-strict-aliasing -o hello -lm
$ strip hello
$ ./hello
Hello, world!
$ size hello
   text	   data	    bss	    dec	    hex	filename
 287958	    536	  12876	 301370	  4993a	hello

This program is now fully self-contained:

$ ldd hello
	linux-gate.so.1 =>  (0xffffe000)
	libm.so.6 => /lib/tls/libm.so.6 (0x4002e000)
	libc.so.6 => /lib/tls/libc.so.6 (0x40050000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

You can also try Using the GNU Autotools with compiled code.

--

Performing the compile above takes about 30sec for me. It seems that this can be sped up by precompiling runtime.c and library.c

$ gcc -c runtime.c library.c -Os -fomit-frame-pointer -fno-strict-aliasing

then compile hello like so:

$ gcc hello.c runtime.o library.o -Os -fomit-frame-pointer -fno-strict-aliasing -o hello -lm

--

Question: why is this so much smaller than compiling statically with libchicken.a?

Because we also link in at least the extras unit (unless -explicit-use is given) with libchicken.a.

Question: How about any eggs I might be using?

OS X

On PowerPC OS X, adding -C -mdynamic-no-pic to csc's options (Or passing -mdynamic-no-pic directly to gcc) will reduce the size of executables and other compiled files that aren't going to be dynamically loaded.