Re: Object Factory and pre-main registering of classes
On May 11, 7:43 am, blargg....@gishpuppy.com (blargg) wrote:
Paavo Helde wrote:
John <gh14...@yahoo.com> kirjutas:
I am trying to create an object factory, AFactory, that
allows new object types (of base class A) to be added
without changing the factory.
The factory is designed so that each class registers
itself with the factory by passing an object name and a
pointer to a function that creates that particular object
type. I am trying to get the classes to register
themselves before the main program executes.
[...]
3. If I compile files separately, link the factory and
related classes into a library and then link the main
routine with the library
g++ -c A*.cpp
ar rvs liba.a A*.o
g++ main.cpp -L. -la
the classes do not register themselves before the main
function.
This is an expected feature of static libraries - everything
not explicitly referenced by the client code is not linked
into the final exeutable.
Just a note: this is practically the definition of what makes
something a library. Static or not. (Despite it's name, a dll
is NOT a library, but a form of an object file.)
And allowed by the C++ standard, which merely guarantees that
each static-duration objects will be initialized before the
first function in its translation unit is called.
Sort of. In practice, no implementation uses this freedom,
because it is impossible to implement in a conforming manner
when cycles are present.
If you never call any functions in that unit, then none of the
static objects need to be initialized. Without this provision,
any translation units with statics involving dynamic
initialization would have to be linked into a program, even if
never otherwise called by it.
That's the case, period. Unless something is linked into the
program, it's not part of the program. The question is only how
to tell the linker that something should be part of the program.
The standard doesn't address how to invoke the compiler or
linker in any way, so this question is out of the realm of the
standard.
This would require large libraries to be broken into lots of
tiny elements that had to be manually selected for linking, or
bloat every program even if only a small part of the library
was used.
I'm afraid I don't see this. The rules for libraries are the
same, regardless of the size of the library.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34