Search code examples
c++static-librariesstatic-linking

Static variable initialization over a library


I am working on a factory that will have types added to them, however, if the class is not explicitly instiated in the .exe that is exectured (compile-time), then the type is not added to the factory. This is due to the fact that the static call is some how not being made. Does anyone have any suggestions on how to fix this? Below is five very small files that I am putting into a lib, then an .exe will call this lib. If there is any suggestions on how I can get this to work, or maybe a better design pattern, please let me know. Here is basically what I am looking for

1) A factory that can take in types

2) Auto registration to go in the classes .cpp file, any and all registration code should go in the class .cpp (for the example below, RandomClass.cpp) and no other files.

BaseClass.h : http://codepad.org/zGRZvIZf

RandomClass.h : http://codepad.org/rqIZ1atp

RandomClass.cpp : http://codepad.org/WqnQDWQd

TemplateFactory.h : http://codepad.org/94YfusgC

TemplateFactory.cpp : http://codepad.org/Hc2tSfzZ


Solution

  • As a general rule of thumb, an application do not include static or global variables from a library unless they are implicitly or explicitly used by the application.

    There are hundred different ways this can be refactored. One method could be to place the static variable inside function, and make sure the function is called.