Global variable from a library not yet initialized when used
Question: ## **The context :**

I created a module (I call it Hello) for Python in C++. The interface between
C++ and Python is made by Swig. It generates a dynamic library `_Hello.so` and
a Python file `Hello.py`. Then, when created, I juste have to call it by this
way :

    
    
    python
    >>> import Hello
    >>> Hello.say_hello()
    Hello World !
    >>>
    

I want to protect this module with a license, then I want the license is
loaded when the module is imported.

## **My implementation :**

In order to be sure the license is loaded when I import the module, I created
a singleton instanciating a global variable :

File `LicenseManager.hpp` :

    
    
    class LicenseManager
    {
      private:
        static LicenseManager licenseManager; // singleton instance
      public:
        static LicenseManager& get(); // get the singleton instance
    
      public:
        LicenseManager();  // load the license
        ~LicenseManager();  // release the license
      private:
        LicenseManager(const LicenseManager&);  // forbidden (singleton)
        LicenseManager& operator = (const LicenseManager&); // forbidden (singleton)
    };
    

File `LicenseManager.cpp` :

    
    
    LicenseManager LicenseManager::licenseManager;  // singleton instance
    
    LicenseManager& LicenseManager::get()
    {
      return LicenseManager::licenseManager;
    }
    
    LicenseManager::LicenseManager()
    {
      /*
       * Here is the code to check the license
       */
      if(lic_ok)
        std::cout << "[INFO] License found. Enjoy this tool !" << std::endl;
      else
      {
        std::cerr << "[ERROR] License not found..." << std::endl;
        throw std::runtime_error("no available licenses");
      }
    }
    
    LicenseManager::~LicenseManager()
    {}
    

This works perfectly ! When I load my module, I obtain :

    
    
    python
    >>> import Hello
    [INFO] License found. Enjoy this tool !
    >>> Hello.say_hello()
    Hello World !
    >>>
    

## **My problem :**

Actually, the code checking the license in my constructor uses the library
_Crypto++_. I have a segmentation fault coming from this library but the same
code in the main function works perfectly. Then I think that Crpyto++ also use
global variables and these are not yet initialized when the constructor of
`LicenseManager` is called.

I know there is no control on the initialization order of the global variables
in C++. But maybe there is another way to do what I need better ?

## **Bad solutions :**

  * If the singleton is not initialized as a global variable, the module is not protected when imported. A solution could be add a check in each functionnality of the module, and force the user to call the LicenseManager before usage. It's not a solution for me because I can't rewrite all my module to impose that (the module is quite huge actually).
  * In the Swig configuration file, I can add Python code called when the module is loaded. Then I have to compile `Hello.py` to prevent the user simply remove the call to the LicenseManager. But then it's a poor security because Python is easy to decompile.


Answer: Answering my own question.

Actually, it is possible with Swig to add C++ code called during the
initialization of the library. Then I just had to add the following code in
the Swig file :

    
    
    %init%{
      LicenseManager::get();
    %}
    

