Page 1 of 1

cross-compiled program size is too big,

Posted: Tue Aug 19, 2008 4:27 am
by crasher
I am using Ubuntu 7.10. By following the OS specific tool chain, I make a cross compiler
together with newlib. I provided a few stubs in syscalls.c which are as simple as one
statement "return -1;"
However, a simple cross compiled program using this cross gcc is 119213 bytes while
the program compiled by the normal gcc installed together with Ubuntu 7.10 is only 6451bytes.
By checking the disassembled code I found unused stubs were also integrated in the
program. Other major size-contributors are <__sprint_r> and <_dtoa_r> which should comes
from newlib.

Is there a way to reduce the cross-compiled program size?

Re: cross-compiled program size is too big,

Posted: Tue Aug 19, 2008 5:43 am
by Combuster
You're comparing a dynamic linked binary to a static linked binary, which isn't really fair.

Normally when the linker links against a library it will look at what symbols are needed, pull those object files from the library, then check again if there are more symbols that can be resolved from that library.

However, if you tell the linker to link it as an input file, it will simply get everything from the object file and put it into the final executable. If your library is not prefixed with the -l switch then this is probably your problem.

Another cause are that too many unrelated calls are put into the same object file (and are hence pulled in together when only part of it is needed)

Using dynamic linking (like linux' native compiler probably did) will simply put no C library code into the final program, but rather pointers to libc.so. This reduces the size of the individual program but will still require you to include libc.so so in reality the total size is larger (dynamic.bin + libraries > static.bin). However if you have many programs, then you need to provide the libc.so only once instead of for each program, and on average you win lots of bytes. Dynamic linking is however much more involved.