代码之家  ›  专栏  ›  技术社区  ›  Rob Lao

动态\u强制转换lt\u dlopen(libtool)加载的共享库中的接口不起作用

  •  2
  • Rob Lao  · 技术社区  · 15 年前

    接口继承如下:

    typedef struct _rwd_plugin_root_t RWD_PLUGIN_ROOT_T;
    
    struct RWD_PLUGIN_API _rwd_plugin_root_t
    {
        virtual int add_ref() = 0;
        virtual int release() = 0;
    }; 
    
    typedef struct _rwd_plugin_base_t RWD_PLUGIN_BASE_T;
    
    struct RWD_PLUGIN_API _rwd_plugin_base_t : _rwd_plugin_root_t
    {
        virtual RWD_PLUGIN_TYPE_T get_plugin_type() = 0;
        virtual const char * get_plugin_label_a() = 0;
        virtual const wchar_t * get_plugin_label_w() = 0;
    };
    
    typedef struct _rwd_autocomplete_plugin_base_t RWD_AUTOCOMPLETE_PLUGIN_BASE_T;
    
    struct RWD_PLUGIN_API _rwd_autocomplete_plugin_base_t : _rwd_plugin_base_t
    {
        virtual int set_proxy(int type, const char * host, long port) = 0;
        virtual int set_term(const char * text) = 0;
        virtual int set_term(const wchar_t * text) = 0;
        virtual int get_phon(std::vector<std::string> & phons) = 0;
    ... // omitted it's too long
    };
    

    然后我在插件中有一个类来实现这样的接口:

    class RWD_PLUGIN_API _rwd_dictcn_t : public _rwd_autocomplete_plugin_base_t
    {
    public:
        _rwd_dictcn_t();
        ~_rwd_dictcn_t();
    ... // details of implementation omitted
    

    插件中的创建者定义如下:

    EXTERN_C int RWD_PLUGIN_API create_rwd_plugin(_rwd_plugin_base_t ** pp)
    {
        *pp = new _rwd_dictcn_t();
        return OK;
    }
    

    最后,我在主应用程序中使用creator,这样使用插件:

    ...
        lt_dlhandle lh = lt_dlopen(filePath);
            RWD_PLUGIN_CREATE_FUNC_T pPluginFunc = NULL;
            if(lh)
            {
                pPluginFunc = reinterpret_cast<RWD_PLUGIN_CREATE_FUNC_T>(lt_dlsym(lh, "create_rwd_plugin"));
    
                if(pPluginFunc)
                {
                    RWD_PLUGIN_BASE_T * pBase = NULL;
                    if(OK == (*pPluginFunc)(&pBase))
                    {
                        RWD_PLUGIN_TYPE_T pluginType = pBase->get_plugin_type();
                        if(pluginType == RWD_PLUGIN_TYPE_AUTOCOMPELE)
                        {
    ...
                            RWD_PLUGIN_FUNC_T pPluginInitFunc = reinterpret_cast<RWD_PLUGIN_FUNC_T>(lt_dlsym(lh, "initialize_rwd_plugin"));
                            if(pPluginInitFunc)
                                (*pPluginInitFunc)(NULL);
    
                            //  set proxy
                            RWD_AUTOCOMPLETE_PLUGIN_BASE_T * pAuto = dynamic_cast<RWD_AUTOCOMPLETE_PLUGIN_BASE_T*>(pBase);
    
    ...
    

    但是,WIN32版本运行良好。 这个问题发生在使用autoconf2.61 automake1.10.1 make3.81 g++4.4.4 libtool1.5.26的linux上。 我在linux编程方面的经验较少,希望能在这里得到帮助。谢谢!

    如果需要,可以在Sourceforge上获取完整的源代码: https://rdwtwdb.svn.sourceforge.net/svnroot/rdwtwdb rdwtwdb公司

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sam Miller    15 年前

    -Wl,--export-dynamic 链接器参数。我记得当遇到类似的行为时,我需要这个论证。

        2
  •  0
  •   bshields    15 年前

    您的问题可能是私有继承:

    _rwd_autocomplete_plugin_base_t : _rwd_plugin_base_t

    编辑 :抱歉,刚意识到你在使用 struct 因此默认继承是公共的。但最好是明确的,特别是因为您看到了编译器之间的差异。