lundi 26 mars 2018

Implicit use of symbol from a static library

I come to submit a problem I have while trying to import symbols from static library implicitly.

Let me set up the problem with a peace of simplified code:

I have 3 projects: - a static plugin handler project that defines a registry class, it is linked in the other two projects - a static plugin project - a final project that has a plugin handler and "load statically" the plugin.

first, the static plugin handler project:

struct PluginType
{
    virtual void doMyStuff() = 0 ;
};

typedef PluginType* (*PluginCreator)();

struct Registry
{
      std::map<std::string, PluginCreator> _item_list;
      static Registry& instance()
      {
            static Registry singleton;
            return singleton;
      }
      static PluginType* create(std::string const& name)
      {
      // call the creator function that returns an PluginType*
        return instance()[name]();
      }
};

struct RecordInRegistry
{
     RecordInRegistry(std::string const& key, PluginCreator new_item_creator)
     {
          Registry::instance()._item_list[key] = new_item_creator;
     }
};

Now, a static plugin project

struct ADerivedItem : public PluginType
{
    virtual void doMyStuff() override
    {
        //some really interesting stuffs
    }
    static PluginType* create() { return new ADerivedItem() ; }
}


export "C" const RecordInRegistry i_record_derived_item_class("derived_item", &ADerivedItem::create);

Then, the final project where i use the plugin. This project links the two other porjects !

int main()
{
    PluginType*  my_instance = Registry::create("derived_item");

    return 0;
}

What I hope is that I will be able to register my static plugin just by linking the static library plugin to the project.

It almost worked ! The only problem I got is that the symbol "i_record_derived_item_class" from the static plugin is not explicitly used in the final project, thus this symbol is not imported from the static plugin lib !

I work on visual studio, i found a simple fix that consists in forcing the symbol import, something like /INCLUDE:i_record_derived_item_class, and everything works fine.

I want to know if you see a work around to this, what I would like is to be able to add this kind of static plung just by linking it, without having to add anything more than /LINK:myStaticPlugin.lib

Any idea ?

Moreover it would be really nice to avoid MSVC-specific solution.

Thanks in advance !

Aucun commentaire:

Enregistrer un commentaire