我有一个应用程序可以利用在运行时使用dlopen加载的插件。每个插件定义一个函数来检索使用通用结构定义的插件信息。类似的东西:dlopen/dlsym尽可能少链接
struct plugin {
char *name;
char *app_version;
int app_verion_id;
char *plugin_version;
int plugin_version_id;
/* ... */
};
struct plugin p = { "sample plugin",APP_VERION,APP_VERSION_ID,"1.2.3",10203 };
struct plugin *get_plugin() {
return &p;
}
这很好,插件可以加载。现在我想构建一个小工具来读取这些属性,而无需链接整个应用程序。为了做到这一点,我有这样的代码:
void *handle;
struct plugin *plugin;
struct plugin *(get_plugin*)();
handle = dlopen(filename, RTLD_LAZY);
if (!handle) { /*...return; ...*/ }
get_plugin = dlym(handle, "get_plugin");
if (!get_plugin) { /*...return; ...*/ }
plugin = get_plugin();
printf("Plugin: %s\n", plugin->name);
这对于简单的插件很好。问题在于许多插件引用了来自应用程序的更多符号,即使设置了RTLD_LAZY,也可以解析这些符号。 (如应用程序中用于初始化插件全局事物的全局变量)因此,dlopen()调用失败,并出现如fatal: relocation error: file sample_plugin.so: symbol application_some_symbol: referenced symbol not found
这样的错误。因为我只想访问单个简单结构,所以我想知道如何阻止链接器完成他的大部分工作。
构建插件时使用了哪些链接选项? – 2010-04-10 17:40:16