像他说的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个问题与此解决方案:
- 有一个非const静态变量:
symbols
宣布plugin.c中的- >非线程安全。
- 在 main()之前执行的代码很难调试。
通常,要加载的符号的名称是预先商定的;以及它的类型。例如,惯例可能是对于名为'foo'的插件,您希望具有'setup_foo','teardown_foo'和'go_foo'函数。 –