2013-03-29 28 views
0

我使用libltdl来动态加载插件库。一直在关注这个documentation,我称之为访问插件中的符号列表

lt_dlhandle lt_dlopen (const char *filename) 

后,我需要知道的符号在这个库中定义的东西。我需要将符号列表传递给

void * lt_dlsym (lt_dlhandle handle, const char *name) 

这需要一个符号名称作为参数。

在我的插件中获取可加载导出符号列表的方法是什么?

+1

通常,要加载的符号的名称是预先商定的;以及它的类型。例如,惯例可能是对于名为'foo'的插件,您希望具有'setup_foo','teardown_foo'和'go_foo'函数。 –

回答

1

像他说的Matthieu M.在他的评论中,没有从动态库获取加载符号列表的本地方法。

但是,我通常使用这种解决方法,即让插件声明容器中的符号,然后从主程序中检索此容器。

plugin.h

#include <set> 
#include <string> 

// call this method from your main program to get list of symbols: 
const std::set<std::string> & getSymbols(); 

void MySymbol01(); 
bool MySymbol02(int arg1, char arg2); 

plugin.c

#include "plugin.h" 

class SymbolDeclarator { 
    public: 
    static std::set<std::string> symbols; 
    SymbolDeclarator(const std::string & symbol) { 
     symbols.insert(symbol); 
    } 
}; 

const std::set<std::string> & getSymbols() { 
    return SymbolDeclarator::symbols; 
} 

#define SYMBOL(RETURN, NAME) \ 
    static const SymbolDeclarator Declarator##NAME(#NAME); \ 
    RETURN NAME 

SYMBOL(void, MySymbol01)() { 
    // write your code here 
} 

SYMBOL(bool, MySymbol02)(int arg1, char arg2) { 
    // write your code here 
} 

我只看到2个问题与此解决方案:

  1. 有一个非const静态变量:symbols宣布plugin.c中的- >非线程安全。
  2. 在 main()之前执行的代码很难调试。