|How do linkers deal with C++ duplicate code? firstname.lastname@example.org (John R Levine) (1998-08-20)|
|Re: How do linkers deal with C++ duplicate code? email@example.com (1998-08-20)|
|Re: How do linkers deal with C++ duplicate code? firstname.lastname@example.org (David Stes) (1998-08-20)|
|Re: How do linkers deal with C++ duplicate code? email@example.com (Urs Hoelzle) (1998-08-20)|
|Re: How do linkers deal with C++ duplicate code? firstname.lastname@example.org (David L Moore) (1998-08-22)|
|Re: How do linkers deal with C++ duplicate code? email@example.com (1998-08-22)|
|Re: How do linkers deal with C++ duplicate code? firstname.lastname@example.org (David Stes) (1998-08-22)|
|Re: How do linkers deal with C++ duplicate code? email@example.com (1998-08-22)|
|Re: How do linkers deal with C++ duplicate code? firstname.lastname@example.org (1998-08-22)|
|[8 later articles]|
|From:||David Stes <email@example.com>|
|Date:||20 Aug 1998 22:56:10 -0400|
|Keywords:||linker, C++, comment|
John R Levine wrote:
> -- Global initializers and destructors I understand, too [...]
There exists an alternative to the approach for global initializers,
as you described it, that may be interesting.
I have no idea whether this alternative is a well-known trick or not ...
I personally think it's rather nice, but in fact, many people consider
it a horrible and terrible hack ! (I admit that at first, I also
thought it was rather fishy).
It's a way to support global initializers by using an (old) feature
that already is available in many C linkers, namely the common storage
linkage model (so that no new linker feature is needed).
You can have each module define a global as its initializer routine,
and all other modules declare the same global as an _uninitialized_
For example, a module "foo" can define the global F as its initializer
and declares the global B as an uninitialized global, while a module
"bar" does the opposite, and declares F as uninitialized and defines B
as its initializer.
The linker on most (but not all) Unix systems will then _merge_ the
_multiple_ definitions for the globals, and use the unique definition,
or use zero (0) for those multiply defined globals that remain
uninitialized, depending on what modules where specified on the link
Like this, you don't have to collect the initializers when linking,
you just link all modules together, and depending on which modules
were specified on the command line (when linking), the program will
find at runtime some symbols to be zero (so you don't have to call
those routines) and some symbols will be non-zero (those are the
global initializers to call).
There's an Objective-C package that uses this trick on some platforms,
[I don't mind the grossness, I used to hand-punch 360/20 object code
into cards on keypunches and you can't get much grosser than that.
But this sounds awfully fragile, you have to hand-enumerate every
possible thing that might have an initializer somewhere and it won't
complain if you forget something. -John]
Return to the
Search the comp.compilers archives again.