Re: C++: conditional static data member possible?
- From: Paul Pluzhnikov <ppluzhnikov-nsp@xxxxxxxxxxx>
- Date: Fri, 24 Nov 2006 17:23:33 -0800
Carlos Moreno <moreno_at_mochima_dot_com@xxxxxxxxxxxxxx> writes:
Paul Pluzhnikov wrote:
Here is a (GNU-ld specific) solution:
Why do I have such guarantee??
Nothing in life is guaranteed, except death and taxes :-)
Don't these definitions clash if
for some reason I need *both* linked?
You will *always* have both linked -- you want to have absolute
minimum of code in the non-shared libfoo_nonshared.a, so you'll
put one data item there (A::used), and also one function the
constructor.
If anyone references the constructor (even in the
if (false) { .... }
section [1]), then you'll get both the constructor, and the data
(because linker doesn't "sub-divide" objects when it pulls them
from archive -- you get all or nothing.
The reason you don't get multiply-defined symbol error [2] is
that UNIX shared libraries are explicitly designed to emulate
archive libraries, and with archives, if you put need definition
info libbar.a and put libbar.a before libfoo.a on the link line,
then libbar.a definition will be used.
Actually, you could eliminate the A::used definition in libfoo_s.so
altogether, by using "weakly-unresolved" reference:
extern int A::used __attribute__((weak));
Then you'll have to modify the condition:
if (&A::used && A::required_action_not_performed) {
}
It may appear that '&A::used' is always true (and some versions of
gcc warn about it), but it's not.
Cheers,
[1] Note that some versions of gcc will optimize out even the
call to the constructor, if gcc can prove that the condition is
always false.
But then, does this program really reference A ?
[2] I did not actually test this, but I am fairly certain it works.
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
.
- References:
- C++: conditional static data member possible?
- From: Carlos Moreno
- Re: C++: conditional static data member possible?
- From: Paul Pluzhnikov
- Re: C++: conditional static data member possible?
- From: Carlos Moreno
- C++: conditional static data member possible?
- Prev by Date: Re: Blocking read() from a USB->Serial adapter.
- Next by Date: Re: Blocking read() from a USB->Serial adapter.
- Previous by thread: Re: C++: conditional static data member possible?
- Next by thread: Signals and threads
- Index(es):
Relevant Pages
|