gcc -static pulls in entire libs

Serban Simu serban@digitalfountain.com
Thu Jan 17 12:12:00 GMT 2002


Hi,

I found that "gcc -static" creates oversized binaries, possibly pulling in
entire libraries rather than the necessary sections.

I have the object file s.o, which uses libmath. I link it as follows, to
produce the binaries s1, s2 and s3:

    $ gcc -o s1 s.o /usr/lib/libm.a
    $ gcc -o s2 -static s.o /usr/lib/libm.a
    $ gcc -o s3 -static s.o -lm
    $ ls -l s1 s2 s3
    -rwxr-xr-x    1 serban   serban       5920 Jan 17 03:23 s1
    -rwxr-xr-x    1 serban   serban     443521 Jan 17 03:23 s2
    -rwxr-xr-x    1 serban   serban     443521 Jan 17 03:23 s3

I'm using gcc version 2.95.4 20010703 (Debian prerelease)

This behavior seems incorrect. Moreover, in case the system provides both .a
and .so versions of the libraries, there is no workaround for using the -lm
syntax. Without -static gcc will build the dynamically linked binary, and
with -static it builds an oversized binary. "strip" only reduces its size a
little bit:

    $ strip s2
    $ ls -l s2
    -rwxr-xr-x    1 serban   serban     397536 Jan 17 03:48 s2

Is there a way to use the -lm syntax, with the -static option, but create
the small binary? Are there any other options that will help not pull in the
entire lib?

I would appreciate any info to help me understand this. Thank you!

Serban Simu
Software Eng., Digital Fountain



More information about the Gcc-help mailing list