10 Writing small programs
Writing very small programs is a trivial thing - if you know how to do it.
Here are some basic tips and tricks:
- Do not use printf:
- ‘printf’ is the most high-level function of
usual ANSI C libraries and builds on almost everything else. Often this function
can be replaced by simpler routines like e.g. ‘puts’. The same is true
for the other formatted I/O functions.
If you write amiga-only programs you can use the ‘sprintf’ routine of
libamiga.a by linking with ‘-lamiga’.
- Try to avoid using buffered I/O:
- A lot of programs can be written using the low-level I/O-functions ‘read’ and
‘write’. This saves the code responsible for buffered I/O.
- Avoid -O3:
- ‘-O3’ activates all those optimizations that sacrifice code size for speed.
An exception is ‘-fstrength-reduce’ which costs compile time but not
code size. ‘-O2 -fstrength-reduce’ will give small programs.
- Strip your executables:
- gcc doesn't strip executables by default. Setting ‘-s’ will give worse
debugable but small programs.
- Use small code and data model:
- ‘-msmall-code -fbaserel’ do this. Be aware that this won't work for
- Don't use libgcc.a:
- ‘libgcc.a’ which calls global C++ constructors and destructors builds
on ‘atexit’ which builds on ‘malloc’, ... If your program doesn't
use them you can save some memory by linking by hand (‘-nostdlib’).
- Set __nocommandline:
- If you use ‘ReadArgs’ instead of ‘argc, argv’ you can save some
memory by setting a global variable named ‘__nocommandline’ to drop
parsing of commandline arguments.