像他說的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'函數。 –