Hi Dave, > * if foo.o is linked directly into an executable, f's ctor is run before > "main". > * if foo.o is linked into a .a and then that .a is linked into an > executable, > f's ctor may or may not be run before "main". The constructor is only run if > there's at least one symbol in foo.o that's referenced by the main program.
Right - this is the intended behaviour. > Note that although the C++ object f isn't used, f's ctor may have interesting > side-effects that need to happen; the parameters may reference things that > need > to be linked (in my case I was using it to register unit testing functions > with > a test runner). Unfortunately the linker is unable to detect these interesting side-effects, and so it cannot know that the object's constructor is actually needed. > I don't know if this behavior is a bug, but this is sufficiently surprising to > me that I wanted to capture it somewhere with a URL; it seems like a "gotcha" > to me (it took me a couple of days of stepping through the link of gcc to > track down). Did you read the linker documentation when you were looking for an answer ? I was wondering whether it would be worthwhile adding a paragraph there to cover this situation, in the hopes of helping future programmers. Cheers Nick _______________________________________________ bug-binutils mailing list bug-binutils@gnu.org https://lists.gnu.org/mailman/listinfo/bug-binutils