public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [BUG] ld behavior varies for C++ static initializer depending on .a or .o input
@ 2003-04-11  3:43 Hal Black
  2003-04-11  4:47 ` Alexandre Oliva
  2003-04-11  8:16 ` Alan Modra
  0 siblings, 2 replies; 17+ messages in thread
From: Hal Black @ 2003-04-11  3:43 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1442 bytes --]

Using GNU ld version 2.13.90.0.18 20030206 (comes with Redhat 9)
Also experienced with previous 3.x and 2.9x versions.

When linking with a bunch of .o files, the desired behavior (static 
initializers are called) happens.  But when linking .a files composed of 
those .o files, static initializers are not called.  On the other hand, 
when the .cpp files are combined to make a single .o file, which is then 
  used to form a .a file, that works.

This was previously reported to gcc bugs, but they sent me here (I've 
been lurking for quite a while to see if it was okay to post bugs here):
http://gcc.gnu.org/cgi-bin/gnatsweb.pl (bug 7769)

There is some good discussion there.

Full source code and makefile follows in the attachment.  See below for 
a quick glance of what I'm talking about.

one.cc
------
#include <stdio.h>

class A {
public:
	A() { printf("Hello world!\n"); }
};
A a;
------

two.cc
------
int main() {}
------

combined.cc
-----------
#include "one.cc"
#include "two.cc"
-----------

Makefile
--------
CPP=g++
test:main1 main2 main3 main4
	./main1
	./main2
	./main3
	./main4
%.o:%.cc
	$(CPP) -c $<
lib%.a:%.o
	ar rv $@ $<
both.a:one.o two.o
	ar rv $@ $^
main1:one.o two.o
	$(CPP) one.o two.o -o main1
main2:libone.a libtwo.a
	$(CPP) libone.a libtwo.a -o main2
main3:both.a
	$(CPP) both.a -o main3
main4:libcombined.a
	$(CPP) libcombined.a -o main4
clean:
	rm -f *.a *.o main*
--------


thanks for your time.
   Hal

[-- Attachment #2: static_init.tar.bz2 --]
[-- Type: application/octet-stream, Size: 554 bytes --]

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2003-04-14 18:41 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-11  3:43 [BUG] ld behavior varies for C++ static initializer depending on .a or .o input Hal Black
2003-04-11  4:47 ` Alexandre Oliva
2003-04-12  4:01   ` Hal Black
2003-04-12  5:43     ` Alexandre Oliva
2003-04-12 14:24       ` Hal Black
2003-04-12 16:52         ` Ian Lance Taylor
2003-04-12 18:01           ` Hal Black
2003-04-12 20:45             ` Alexandre Oliva
2003-04-13  2:11               ` Hal Black
2003-04-14 18:41                 ` Alexandre Oliva
2003-04-13 22:55             ` Ian Lance Taylor
2003-04-13 23:07               ` Zack Weinberg
2003-04-13 23:15                 ` Ian Lance Taylor
2003-04-13 23:26                 ` Ulrich Drepper
2003-04-14  5:14               ` Hal Black
2003-04-14  5:30                 ` Ian Lance Taylor
2003-04-11  8:16 ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).