我有一个小的查询,如下所示。
我从下面的代码创建了一个共享库。
帮助
#include<iostream>
#include<signal.h>
#include<unistd.h>
using namespace std;
void killMe(int sig_num);
void printMe(void);
help.cpp
#include<iostream>
#include<signal.h>
#include<unistd.h>
using namespace std;
void killMe(int sig_num)
{
cout<<"Timeout occurred."<<endl;
raise(SIGKILL);
}
void printMe()
{
cout<<"This is help.cpp"<<endl;
}
[root@localhost DL]# nm -n /usr/local/lib/libmyhelp.so | grep " T "
00000584 T _init
00000760 T _Z6killMei
000007ae T _Z7printMev
00000864 T _fini
[root@localhost DL]#
检查nm的输出,我发现killMe和printMe函数的名称已稍作更改。有什么办法可以在共享库中保留与cpp代码中相同的名称?谢谢。
最佳答案
这是由于C++ name mangling。要关闭它,请将函数声明为extern "C"
。
help.h:
#include<iostream>
#include<signal.h>
#include<unistd.h>
using namespace std;
extern "C" {
void killMe(int sig_num);
void printMe(void);
}
关于c++ - 库中的函数名称已更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14291854/