[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug ld/20152] constructors for c++ objects are sometimes discarded
From: |
Nick Clifton |
Subject: |
Re: [Bug ld/20152] constructors for c++ objects are sometimes discarded when linked via a .a archive |
Date: |
Fri, 3 Jun 2016 08:30:37 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 |
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