I worked and tested self-registration, an abstract factory based on the one described here:
stack overflow
In all my test cases, it works like a charm and provides the features and reuse that I wanted.
Binding in this factory in my project using cmake was quite complicated (although it seems to be more of an ar problem).
I have identical base.hpp, b.pp / cpp derivatives and equivalent .hpp / cpp derivatives to the linked example. Basically, I just create an instance of the factory and call createInstance () twice, each time with "DerivedA" and "DerivedB".
The executable file created by the line:
g++ -o testFactory main.cpp derivedb.o deriveda.o
works as expected. Moving my derived classes to the library (using cmake, but I also tested this with ar), and then communication failed:
ar cr libbase.a deriveda.o derivedb.o g++ -o testFactory libbase.a main.cpp
calls only the first static instantiation (from a derivative of A.cpp) and never the second static instantiation, i.e.
// deriveda.cpp (if listed first in the "ar" line, this gets called) DerivedRegister<DerivedA> DerivedA::reg("DerivedA"); // derivedb.cpp (if listed second in the "ar" line, this does not get called) DerivedRegister<DerivedB> DerivedB::reg("DerivedB");
Note that replacing two in the string ar causes only a static instantiation of the derived bb.cpp, not an instance of the derived .cpp.
Am I missing something with ar or static libraries that somehow don't play with static variables in C ++?
Ethan coon
source share