使用静态单例class实例导致动态加载失败
Using static singleton class instance causes dynamic loading to fail
我正在创建一个共享库,它导出一个使用静态 class 实例的函数。共享库旨在动态加载。但是,出于某种原因,使用静态 class 实例会导致动态加载失败。我正在使用 Poco 加载库。
//my_library.h
#ifndef MY_LIB_H
#define MY_LIB_H
#define DUMMY_MSG 1;
class SendClass
{
public:
SendClass() = default;
SendClass( const SendClass &other ) = delete;
SendClass &operator=( const SendClass &other ) = delete;
static SendClass *get_instance(){ return &singleton_instance; };
int send(){ return DUMMY_MSG; };
private:
static SendClass singleton_instance;
};
extern "C" __attribute__ ((visibility ("default"))) int send();//this is the function to be exported
inline int send()
{
return SendClass::get_instance()->send();
}
#endif // MY_LIB_H
我用下面的命令将上面的头文件编译成共享库,并把库放在/tmp/下
g++ -shared -fPIC -o libexp.so my_library.h
然后我尝试在我的主程序中加载库
//main.cpp
#include "Poco/SharedLibrary.h"
using namespace std;
typedef int(*SENDFUNC)();
int main
(
int argc,
char **argv
)
{
Poco::SharedLibrary lib;
lib.load( "/tmp/libexp.so" ); //crashes here!
SENDFUNC send_func = (SENDFUNC)lib.getSymbol("send");
int msg = send_func();
return 0;
}
程序在第 "lib.load( "/tmp/libexp.so" );" 行崩溃带有以下消息:
terminate called after throwing an instance of
'Poco::LibraryLoadException' what(): Cannot load library
但是,如果我把SendClass::get_instance的body改成下面这样,动态加载就成功完成了
//if SendClass::get_instance is implemented as follows, dynamic loading succeeds
static SendClass *get_instance(){ return new SendClass; };
那么为什么使用静态实例会导致动态加载失败呢?
根据@ALX23z 的建议,我通过添加以下 cpp 文件解决了这个问题
//my_library.cpp
#include "my_library.h"
SendClass SendClass::singleton_instance;
我正在创建一个共享库,它导出一个使用静态 class 实例的函数。共享库旨在动态加载。但是,出于某种原因,使用静态 class 实例会导致动态加载失败。我正在使用 Poco 加载库。
//my_library.h
#ifndef MY_LIB_H
#define MY_LIB_H
#define DUMMY_MSG 1;
class SendClass
{
public:
SendClass() = default;
SendClass( const SendClass &other ) = delete;
SendClass &operator=( const SendClass &other ) = delete;
static SendClass *get_instance(){ return &singleton_instance; };
int send(){ return DUMMY_MSG; };
private:
static SendClass singleton_instance;
};
extern "C" __attribute__ ((visibility ("default"))) int send();//this is the function to be exported
inline int send()
{
return SendClass::get_instance()->send();
}
#endif // MY_LIB_H
我用下面的命令将上面的头文件编译成共享库,并把库放在/tmp/下
g++ -shared -fPIC -o libexp.so my_library.h
然后我尝试在我的主程序中加载库
//main.cpp
#include "Poco/SharedLibrary.h"
using namespace std;
typedef int(*SENDFUNC)();
int main
(
int argc,
char **argv
)
{
Poco::SharedLibrary lib;
lib.load( "/tmp/libexp.so" ); //crashes here!
SENDFUNC send_func = (SENDFUNC)lib.getSymbol("send");
int msg = send_func();
return 0;
}
程序在第 "lib.load( "/tmp/libexp.so" );" 行崩溃带有以下消息:
terminate called after throwing an instance of
'Poco::LibraryLoadException' what(): Cannot load library
但是,如果我把SendClass::get_instance的body改成下面这样,动态加载就成功完成了
//if SendClass::get_instance is implemented as follows, dynamic loading succeeds
static SendClass *get_instance(){ return new SendClass; };
那么为什么使用静态实例会导致动态加载失败呢?
根据@ALX23z 的建议,我通过添加以下 cpp 文件解决了这个问题
//my_library.cpp
#include "my_library.h"
SendClass SendClass::singleton_instance;