【发布时间】:2012-02-07 01:30:21
【问题描述】:
我发现我可以在共享库中实现类的某些部分,只要符号在使用时被加载。
myclass.h
---
class C {
void method();
}
main.cpp
---
#include "myclass.h"
int main() {
//dynamically load mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
}
mylib.so compiled separately:
====
mylib.cpp
---
#include "mylib.h"
void C::method() {
...
}
这很好用。
但是,一旦我用完 C::method(),我想卸载它,这样我就可以更改、重新编译和重新加载它,而无需重新启动主程序
int main() {
//dynamically load mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
// close the lib, remove the handle...
....
char pause;
cin << pause; // before carrying on change the C::method, recompile it
//dynamically load *Again* mylib.so using dlopen/dlsym/dlclose
...
C *c = new C();
c->method();
delete c;
}
问题是在第一次dlclose时它没有卸载库,可能是因为我的类C的一个实例存在。 有什么办法可以强制这样做?
(在 Linux Ubuntu 10.04 上使用 g++ 4.2.3)
【问题讨论】:
-
我认为没有跟踪到你有一个类的实例(尤其是通过指针)。你怎么知道,库没有卸载?
-
你能在
dlclose之后打电话给dlerror看看为什么库没有被卸载吗? -
@Krizz:尽管我做了一些改变,但行为保持不变。例如。 cout
-
@parapura rajkumar:在“cout
-
你是用
RTLD_GLOBAL打开图书馆吗?这可能会导致其他库在其中使用符号,从而阻止它在关闭时卸载。如果您只想查找带有dlsym的符号,则不需要该标志。
标签: c++ class implementation dlopen