添加到动态库中的函数指针映射
Adding to function pointer map in dynamic library
下面的两个代码部分显示了重要信息。真正的工作在第三个代码部分main.cpp.中完成
// For all files:
typedef void (*func_ptr)(Event&); // Event is a class with certain methods
foo.cpp
extern "C" void init(std::map<std::string, func_ptr> *func_ptrs)
{
std::cout<<std::endl<<"Initializing..."<<std::endl; // does appear in output, so function does execute
(*func_ptrs)["test"] = stuff; // the stuff function below
}
// this is the function we are trying to add to the func_ptrs map
// It looks like it's added in main.cpp, but calling it in main.cpp seg faults
void stuff(Event &e)
{
std::cout<<"In stuff..."<<std::endl; // seg faults before this line
// do things with e
}
main.cpp
// Excluding fluff for brevity, use comments to understand location of problem
// In main....
map<string, func_ptr> func_ptrs; // map of strings to func_ptrs
void (*init)(map<string, func_ptr>*);
// some other stuff
handle = dlopen("libfoo.so", RLTD_NOW);
// library opens successfully
*(void **) (&init) = dlsym(handle, "init"); // get the init function from the library
// No, I don't completely understand the above sequence of pointers and reference, but it does open successfully
(*init)(&func_ptrs);
// Executes successfully
// Iterating over func_ptrs shows that the new "test" mapping is in func_ptrs!!
// Next, this should call the stuff function in foo.cpp that was assigned to func_ptrs["test"]
(func_ptrs["test"])(event); // seg fault
综上所述,
- libfoo.so库正在正确加载
- 正在正确检索libfoo.so中的init符号
- 运行libfoo的init((后,映射键"test"在main.cpp中可见
错误:试图执行libfoo的东西,位于main.cpp的映射func_ptrs中,seg错误
注意:另一种选择是向main.cpp提供一个"添加到映射"函数,libfoo.so可以调用该函数将映射添加到函数列表中。这看起来更容易/更好吗?
我找到了很多类似的问题/答案,但没有一个与我所处的情况有足够密切的联系。非常感谢你们的帮助,我还没有失望。希望我没有把缩短代码搞砸。
此代码适用于gcc版本4.7.2 20121109:
d_a.cc:
#include <iostream>
#include <map>
#include <string>
#include <dlfcn.h>
class Event {
public:
void method1() { std::cout << "method1: " << name << std::endl; }
Event(std::string& n):name(n) {}
private: std::string name;
};
typedef void (*func_ptr)(Event&);
int main(int argc, const char **argv) {
std::string name = "Some";
Event ev(name);
std::map<std::string, func_ptr> func_ptrs;
void (*init)(std::map<std::string, func_ptr>*);
void *handle = dlopen("./d_l.so", RTLD_NOW);
*(void **) (&init) = dlsym(handle, "init");
(*init)(&func_ptrs);
(func_ptrs["test"])(ev);
return 0;
}
d_l.cc:
#include <map>
#include <string>
#include <iostream>
void stuff(Event& e)
{
e.method1();
std::cout<<"In stuff..."<<std::endl;
}
extern "C" void init(std::map<std::string, func_ptr> *func_ptrs)
{
std::cout<<std::endl<<"Initializing..."<<std::endl;
(*func_ptrs)["test"] = stuff;
}
编译:
g++ d_a.cc -ldl
g++ d_l.cc -fPIC -shared -o d_l.so
执行:
./a.out
输出:
Initializing...
method1: Some
In stuff...
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针