Re: Problem with static linking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jul 16, 2009 at 12:46 AM, Ian Lance Taylor<iant@xxxxxxxxxx> wrote:
> Zachary Turner <divisortheory@xxxxxxxxx> 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
>

What happens if you run valgrind on your hello world executable?  My
hello world app works as well, but when I run valgrind on it I get
tons of errors.  The errors could possibly be harmless, but on the
other hand I feel like they are probably an indication of something
serious, because when I run a larger application built in this way the
program appears to work for a while, but later terminates in ways that
make absolutely no sense.  The errors are unexplainable enough that I
can only attribute them to linking/library/configuration problems, and
given that valgrind reports over 70,000 errors on that codebase, all
similar to the valgrind errors I'm seeing when I compile this empty
program with -static, I have to think it's related.

I've experienced this behavior with glibc 2.9 on both ubuntu and Cent
OS with GCC 4.2.4, 4.3.x, and 4.4.0.  I've also experienced this
behavior with gcc compiling .c files as well.  Empty main function,
gcc test.c -static -o test && valgrind ./test.  It's possible I'm
doing something wrong, but I can't imagine what.  I agree that having
-static give an error would be annoying.  But producing an incorrect
binary is pretty annoying too :(

Zach


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux