This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Problem with static linking
- From: Ian Lance Taylor <iant at google dot com>
- To: Zachary Turner <divisortheory at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 15 Jul 2009 22:46:25 -0700
- Subject: Re: Problem with static linking
- References: <478231340907151937x4f1059f8l4545bb071441c9f9@mail.gmail.com> <m3fxcx70qi.fsf@google.com> <478231340907152130q4ea20523r59f837a24b7e120b@mail.gmail.com>
Zachary Turner <divisortheory@gmail.com> writes:
> Thanks for your response despite the fact that I apparently posted to
> the wrong list. However, one thing still bothers me. In particular
> that an out-of-the-box installation of gcc appears to produce bogus
> executables on gnu linux and neither the documentation, the compiler,
> nor the linker issue any kind of warnings stating that this might
> happen (or has already happened). In other words, the -static linker
> option, at least under its current implementation, appears to be
> *incompatible* with g++ (and for that matter, gcc as well) on gnu
> linux. The documentation does state that the -static option behaves
> differently depending on whether or not it's supported by the
> platform. It seems to be unsupported on this platform, so shouldn't
> it simply either report an error that this option is unavailable, or
> produce an executable that is "as static as possible" but at least
> works?
>
> For example,
>
>> g++ -v -static test.cpp -o test
>
> produces an unusable executable.
>
> g++ -v test.cpp -o test -nodefaultlibs -Wl,-Bstatic -lstdc++ -lm -lgcc
> -lgcc_eh -Wl,-Bdynamic -lc
>
> seems to work correctly (I haven't tested it in our large codebase
> yet, just a simple program that throws and catches an exception in
> main). Since the first command line can apparently *never* produce
> correct results, could it be changed to have the same behavior as the
> second command line on gnu-linux?
When I try -static with g++ on a hello, world program on GNU/Linux, the
resulting program does work, in that it prints hello, world. I haven't
tried running valgrind on it. If you find that -static gives you a
completely unusable executable--one that doesn't work at all--then
something may be wrong. I tried gcc 4.3.2 on Fedora Core 10 using glibc
2.9.3.
Given that -static does work with simple executables, and given that the
gcc driver does hardly anything with -static other than pass it to the
linker, I think that gcc is behaving reasonably. It would be annoying
for it to give an error.
Ian