2013-03-29 70 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()之前執行的代碼很難調試。