Since this is C++ code, you shouldn't need C-style hacks.
class LibInstance {
public:
LibInstance() {
qDebug() << __FILE__ << "has been initialized";
}
~LibInstance() {
qDebug() << __FILE__ << "has been unloaded";
}
}
Q_GLOBAL_STATIC(LibInstance, libInstance)
class LibExecutor {
LibExecutor() { libInstance(); }
};
static LibExecutor libExecutor;
An instance of LibExecutor
is guaranteed to be constructed, in a non-threadsafe way, before main()
starts running; or before the library finishes loading, if it's demand-loaded. The LibExecutor
's constructor is then using the Q_GLOBAL_STATIC
implementation to thread-safely execute the LibInstance
's constructor.
It is in LibInstance
's constructor that you place the initialization functionality you desire. The destructor will be called when the library is being unloaded.
It is generally safe, cross platform, and used within Qt itself.