source: project/wiki/generating the smallest possible, self-contained executable @ 37557

Last change on this file since 37557 was 31126, checked in by Mario Domenech Goulart, 6 years ago

Properly capitalize CHICKEN on the wiki directory (only first level).

I used the following shell script to change things:

while IFS= read -d $'\0' -r file ; do

sed -i 's/Chicken/CHICKEN/g' "$file"

done < <(find wiki -maxdepth 1 -type f -print0 )

Some files have been manually reverted after that, since some
substitutions don't apply:

  • friedly-chicken (repl banner)
  • survey2011 (Chicken in URI paths)
  • chickenista-guide (Chickenista)

I hope the link canonicalization thing will be on my side.

File size: 3.1 KB
Line 
1[[toc:]]
2
3== Reducing the size of a compiled executable
4
5=== Generating the smallest possible, self-contained executable
6
7Here is a small demonstration for creating the smallest possible (while still self-contained) executable. A UNIX-like environment is assumed.
8
9A simple hello-world program:
10
11<enscript highlight=scheme>
12(display "Hello, world!\n")
13</enscript>
14
15You need the files {{library.c}}, {{runtime.c}}, {{build-version.c}}, {{chicken.h}} and {{buildtag.h}} from the CHICKEN source distribution.
16
17First we generate {{hello.c}} from {{hello.scm}}:
18
19 $ chicken hello.scm -explicit-use -uses library
20
21The options {{-explicit-use -uses library}} (note the single dashes) tell the compiler not to link in the evaluator, we just want the basic Scheme library (and the runtime system).
22
23Now we compile {{hello.c}}, the runtime system in {{runtime.c}}, the core library {{library.c}} and build version information {{build-version.c}} into a single executable:
24
25 $ gcc hello.c runtime.c library.c build-version.c -Os -fomit-frame-pointer -fno-strict-aliasing -o hello -lm
26 $ strip hello
27 $ ./hello
28 Hello, world!
29 $ size hello
30    text           data     bss     dec     hex filename
31  287958            536   12876  301370   4993a hello
32
33This program is now fully self-contained:
34
35 $ ldd hello
36        linux-gate.so.1 =>  (0xffffe000)
37        libm.so.6 => /lib/tls/libm.so.6 (0x4002e000)
38        libc.so.6 => /lib/tls/libc.so.6 (0x40050000)
39        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
40
41You can also try using the GNU Autotools with compiled code.
42
43Not all functionality provided by {{library.scm}} may be required, though. It is
44possible to use a custom library:
45
46<enscript highlight=scheme>
47;;;; lib.scm
48
49(define (##sys#interrupt-hook reason state) #f)
50(define (##sys#error-hook code loc . args) (##core#inline "C_halt" "error"))
51</enscript>
52
53  $ csc -c hello.scm -x -uses library
54  $ csc -c lib.scm -raw -x -O2 -d0
55  $ gcc -c runtime.c -Os -fomit-frame-pointer -fno-strict-aliasing
56  $ gcc hello.o runtime.o lib.o -Lchicken -lm
57
58Using this library will generate an even smaller executable. Since you will need
59some library functions it will be necessary to copy parts of the original
60{{library.scm}} into your custom library. Doing this will require some knowledge
61of the CHICKEN internals.
62
63--
64
65Performing the compile above takes about 30sec for me. It seems that this can be sped up by precompiling {{runtime.c}} and {{library.c}}
66
67 $ gcc -c runtime.c library.c build-version.c -Os -fomit-frame-pointer -fno-strict-aliasing
68
69then compile hello like so:
70
71 $ gcc hello.c runtime.o library.o build-version.o -Os -fomit-frame-pointer -fno-strict-aliasing -o hello -lm
72
73--
74
75Question: why is this so much smaller than compiling statically with {{libchicken.a}}?
76
77Because we also link in at least the extras unit (unless {{-explicit-use}} is given) with {{libchicken.a}}.
78
79Question: How about any eggs I might be using?
80=== OS X
81
82On 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.
Note: See TracBrowser for help on using the repository browser.