【发布时间】:2020-04-29 03:04:18
【问题描述】:
请考虑以下代码示例。我的问题是:Foo::bar 存储在内存中的什么位置?是存放在main.cpp编译成的程序的静态存储中吗?如果是这样,当 myDll.so 被卸载时会发生什么?
//myDll.hpp
//class definitions
class Bar
{
public:
Bar() = default;
};
class Foo
{
public:
static Bar bar;
};
//declaring free function to be exported
extern "C" Bar* getBar();
//myDll.cpp
#include "myDll.hpp"
//initializing static member variable
Bar Foo::bar;
//definition of exported function
Bar* getBar()
{
return &(Foo::bar);
}
假设 myDll.cpp 像这样被制作成 myDll.so:
g++ -shared -fPIC -o myDll.so myDll.cpp,动态加载如下:
//main.cpp
void* handle = dlopen( "/path/to/dll/myDll.so", RTLD_NOW);
// do stuff with handle...
dlclose( handle );
调用 dlclose() 时会发生什么? Foo::bar 会立即超出范围吗?
【问题讨论】:
标签: c++ dll static dynamic-loading