Search code examples
c++initializationshared-libraries

c++ static initialization chain and shared libraries


I have a simple case of a static initialization chain.

In file Base.h

class Base {
public:
    Base() {
       this->next_ = list_;
       list_ = this;
    }

    virtual void
    Init() = 0;

    static void InitAll() {
        Base* cur = list_;
        while(cur) {
             cur->Init();
             cur = cur->next_;
        }
    }

private:
    Base* next_;
    static inline Base* list_;
};

In file A.cpp


class A: public Base {
public:
     ...
     void Init() override { ... }
};

static A instance_of_a;

I have similar files B.cpp, C.cpp, etc.

I would like to make A.cpp, B.cpp, C.cpp, ... into a separate shared library each, is it possible without using dlsym? Can I put Base into another shared library?


Solution

  • It just works: nothing special is required.