dynamic_cast an interface from a shared library which was loaded by lt_dlopen(libtool) doesn't work
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);
...
The problem is dynamic_cast always fails and pAuto end up being a nil. However the WIN32 version works fine. The problem happened on 开发者_JS百科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!
The full source code could be get on Sourceforge if necessary: svn co https://rdwtwdb.svn.sourceforge.net/svnroot/rdwtwdb rdwtwdb
you might try building with -Wl,--export-dynamic
linker argument. I recall needing this argument when encountering similar behavior.
You're problem may be the private inheritance:
_rwd_autocomplete_plugin_base_t : _rwd_plugin_base_t
Also I think because you have virtual functions your types are already polymorphic, but wouldn't hurt to add virtual destructors.
Edit: Sorry just realized you're using struct
there so the default inheritance is public. But probably best to be explicit, especially since you're seeing differences between compilers.
精彩评论