1. Intro -------- GCC manual defines the following cmd line options meaning: "-static On systems that support dynamic linking, this prevents linking with the shared libraries. -shared Produce a shared object..." 2. The issue ------------ So, if we wish to produce a shared object linked against static libraries, we have to specify "-shared -static" driver options. For instance: $ gcc foo.c -shared -o libfo.so -static But if we check the library built we will see that it is linked against some shared libs. $ ldd ./libfoo.so /lib/libNoVersion.so.1 => /lib/libNoVersion.so.1 (0x40003000) libc.so.6 => /lib/i686/libc.so.6 (0x40015000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x2aaaa000) *** This is not what expected! *** 3. Driver work -------------- Let's check what the GCC compiler driver is doing. We use --verbose to see details. $ gcc foo.c -shared -o libfo.so -static --verbose ... /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/collect2 -m elf_i386 -shared -o libfoo.so /usr/lib/crti.o /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/crtbeginT.o -L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1 -L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/../../.. /home/grigory/tmp/cc6Ymc3Z.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.1.1/crtendS.o /usr/lib/crtn.o We can see a kind of ambiguity here: (a) linker command line does not contain '-static' option requested, but at the same time, (b) the set of libraries "-lgcc -lgcc_eh -lc -lgcc -lgcc_eh" is specific for static build. I.e. we can see the same set while building executable linked against static libraries. 4. Question ----------- So, what is going on here: - Driver just lost '-static' in the linker command line? - Or this is intentional restriction? Release: 3.1.1 Environment: host: i686-pc-linux-gnu build: i686-pc-linux-gnu target: i686-pc-linux-gnu configured with: ./configure --enable-threads
State-Changed-From-To: open->analyzed State-Changed-Why: Confirmed. This is a weird case, still existing in present mainline. W.
I and some others think this should be rejected and the documentation changed.
(In reply to comment #0) > $ ldd ./libfoo.so > /lib/libNoVersion.so.1 => /lib/libNoVersion.so.1 (0x40003000) > libc.so.6 => /lib/i686/libc.so.6 (0x40015000) > /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x2aaaa000) > > *** This is not what expected! *** i'm using a little workaround: gcc libfoo.c -o libfoo.so -shared -fPIC \ -nodefaultlibs -Wl,-Bstatic -lc -lgcc -lgcc_eh -lc $ ldd libfoo.so statically linked $ file libfoo.so libfoo.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped