【问题标题】:Accessing list of symbols in plugin访问插件中的符号列表
【发布时间】:2013-03-29 07:50:51
【问题描述】:

我正在使用 libltdl 来动态加载插件库。一直关注这个documentation,然后我打电话给这个

lt_dlhandle lt_dlopen (const char *filename)

我需要知道这个库中定义了哪些符号。我需要将符号列表传递给

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

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

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

【问题讨论】:

  • 一般来说,要加载的品种名称是预先约定好的;以及它的类型。例如,约定可能是对于名为foo 的插件,您希望具有setup_footeardown_foogo_foo 函数。

标签: c++ dynamic-loading libltdl


【解决方案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. 有一个非常量静态变量:symbols 声明 在 plugin.c -> 非线程安全的。
  2. 在 main(),很难调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2016-09-09
    • 2010-10-04
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多