I need to register classes when the code is loading. I implemented a solution which works great as long as the code is compiled in the app.
But when the code is provided by a static library it doesn't work at all.
I solved this problem in a fashion similar to this answer: https://stackoverflow.com/a/729028/171711.
Currently I have something like:
#define REGISTER(className)\
static const int __classDescriptor##className = MyRegister(#className, className::GetAllocator());
When used it looks like:
//Foo.cpp
REGISTER(Foo);
Foo::Foo()
{
...
}
And I have in the logs:
registered class:Foo
But when I created a static library and Foo is provided by the library the problem is that REGISTER(Foo);
is never called.
I have a complex loading system to allow scripts to use native C++ classes which is dependent on this behavior. Is there a way to force the code in Foo.cpp to execute when the library is loaded?
Edit: It seems my question is directly related to the one about static linking in Visual Studio. It seems I have the same problem with my own libraries. I noticed that some of the classes from the library are registered. And they are only the ones which have their .h
file included in my project.
So is there a way to execute code in a lib without linking to the .h
file?