title: classes and static variables in shared libraries
body: <p>I am trying to write something in c++ with an architecture like:</p>
  
  <p>App --> Core (.so)  &lt;-- Plugins (.so's)</p>
  
  <p>for linux, mac and windows. The Core is implicitly linked to App and Plugins are explicitly linked with dlopen/LoadLibrary to App. The problem I have:
  <li> static variables in Core are duplicated at run-time -- Plugins and App have different copys of them.
  <li> at least on mac, when a Plugin returns a pointer to App, dynamic casting that pointer in App always result in NULL. </p>
  
  <p>Can anyone give me some explanations and instructions for different platforms please? I know this may seem lazy to ask them all here but I really cannot find a systematic answer to this question.</p>
  
  <p>What I did in the entry_point.cpp for a plugin:</p>
  
  <pre><code>#include "raw_space.hpp"
  
  #include &lt;gamustard/gamustard.hpp&gt;
  
  using namespace Gamustard;
  using namespace std;
  
  namespace
  {
    struct GAMUSTARD_PUBLIC_API RawSpacePlugin : public Plugin
    {
      RawSpacePlugin(void):identifier_("com.gamustard.engine.space.RawSpacePlugin")
      {
      }
  
      virtual string const&amp; getIdentifier(void) const
      {
        return identifier_;
      }
  
      virtual SmartPtr&lt;Object&gt; createObject(std::string const&amp; name) const
      {
        if(name == "RawSpace")
        {
          Object* obj = NEW_EX RawSpaceImp::RawSpace;
          Space* space = dynamic_cast&lt;Space*&gt;(obj);
          Log::instance().log(Log::LOG_DEBUG, "createObject: %x -&gt; %x.", obj, space);
          return SmartPtr&lt;Object&gt;(obj);
        }
        return SmartPtr&lt;Object&gt;();
      }
  
    private:
      string identifier_;
    };
  
    SmartPtr&lt;Plugin&gt; __plugin__;
  }
  
  extern "C"
  {
    int GAMUSTARD_PUBLIC_API gamustardDLLStart(void) throw()
    {
      Log::instance().log(Log::LOG_DEBUG, "gamustardDLLStart");
      __plugin__.reset(NEW_EX RawSpacePlugin);
      PluginManager::instance().install(weaken(__plugin__));
      return 0;
    }
  
    int GAMUSTARD_PUBLIC_API gamustardDLLStop(void) throw()
    {
      PluginManager::instance().uninstall(weaken(__plugin__));
      __plugin__.reset();
      Log::instance().log(Log::LOG_DEBUG, "gamustardDLLStop");
      return 0;
    }
  }
  </code></pre>