Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: Empty program == 23202 bytes?

test.c:

int main()
{
    return 0;
}

I haven't used any flags (I am a newb to gcc) , just the command:

gcc test.c

I have used the latest TDM build of GCC on win32. The resulting executable is almost 23KB, way too big for an empty program.

How can I reduce the size of the executable?

like image 814
George Avatar asked Aug 22 '09 12:08

George


4 Answers

Don't follow its suggestions, but for amusement sake, read this 'story' about making the smallest possible ELF binary.

like image 109
Phil Miller Avatar answered Nov 08 '22 18:11

Phil Miller


How can I reduce its size?

  • Don't do it. You just wasting your time.
  • Use -s flag to strip symbols (gcc -s)
like image 36
maykeye Avatar answered Nov 08 '22 19:11

maykeye


By default some standard libraries (e.g. C runtime) linked with your executable. Check out keys --nostdlib --nostartfiles --nodefaultlib for details. Link options described here.

For real program second option is to try optimization options, e.g. -Os (optimize for size).

like image 12
Kirill V. Lyadvinsky Avatar answered Nov 08 '22 20:11

Kirill V. Lyadvinsky


Give up. On x86 Linux, gcc 4.3.2 produces a 5K binary. But wait! That's with dynamic linking! The statically linked binary is over half a meg: 516K. Relax and learn to live with the bloat.

And they said Modula-3 would never go anywhere because of a 200K hello world binary!


In case you wonder what's going on, the Gnu C library is structured such as to include certain features whether your program depends on them or not. These features include such trivia as malloc and free, dlopen, some string processing, and a whole bucketload of stuff that appears to have to do with locales and internationalization, although I can't find any relevant man pages.

Creating small executables for programs that require minimum services is not a design goal for glibc. To be fair, it has also been not a design goal for every run-time system I've ever worked with (about half a dozen).

like image 12
Norman Ramsey Avatar answered Nov 08 '22 20:11

Norman Ramsey