dynamic_cast来自由lt_dlopen(libtool)加载的共享库的接口不工作 [英] dynamic_cast an interface from a shared library which was loaded by lt_dlopen(libtool) doesn't work
问题描述
这是关于我的程序中的插件功能。我需要一个C ++类(和对象)在一个插件可以由主模块通过接口使用。
接口继承如下:
This is about plugin features in my program. I need a C++ class(and object) in a plugin could be used by main module through an interface. The interface inheritance like this:
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
};
然后我在插件中有一个类来实现这样的接口:
Then I have a class in plugin to implement the interface like this:
class RWD_PLUGIN_API _rwd_dictcn_t : public _rwd_autocomplete_plugin_base_t
{
public:
_rwd_dictcn_t();
~_rwd_dictcn_t();
... // details of implementation omitted
插件中的创建者定义如下:
The creator in plugin is defined like this:
EXTERN_C int RWD_PLUGIN_API create_rwd_plugin(_rwd_plugin_base_t ** pp)
{
*pp = new _rwd_dictcn_t();
return OK;
}
最后,我使用主应用程序中的创建者,像这样:
At last, I use the creator in main application so as to use the plugin like this:
...
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);
...
问题是dynamic_cast总是失败,pAuto结束是零。
然而WIN32版本工作正常。
问题发生在linux与autoconf2.61 automake1.10.1 make3.81 g ++ 4.4.4 libtool1.5.26。
我有较少的linux编程经验,希望在这里得到帮助。感谢!
The problem is dynamic_cast always fails and pAuto end up being a nil. However the WIN32 version works fine. The problem happened on linux with autoconf2.61 automake1.10.1 make3.81 g++4.4.4 libtool1.5.26 . I have less experience with linux programming and hope getting help here. Thanks!
如有必要,可以在Sourceforge上获取完整的源代码:
svn co <a href =https://rdwtwdb.svn.sourceforge。 net / svnroot / rdwtwdbrel =nofollow> https://rdwtwdb.svn.sourceforge.net/svnroot/rdwtwdb rdwtwdb
The full source code could be get on Sourceforge if necessary: svn co https://rdwtwdb.svn.sourceforge.net/svnroot/rdwtwdb rdwtwdb
推荐答案
您可以尝试使用 -Wl, - export-dynamic
链接器参数构建。我记得当遇到类似的行为时需要这个参数。
you might try building with -Wl,--export-dynamic
linker argument. I recall needing this argument when encountering similar behavior.
这篇关于dynamic_cast来自由lt_dlopen(libtool)加载的共享库的接口不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!