先把代码贴出来
#ifndef _SO_TEST_H_
#define _SO_TEST_H_
#ifdef __cplusplus
extern "C" {
#endif
int add(int x, int y);
int sub(int x, int y);
#ifdef __cplusplus
}
#endif
#endif // _SO_TEST_H_
#include "so_test.h"
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
编译,nm -D 查看输出结果,可以到看 -fini _init 这些符号也导出了
[root@localhost so_test]# g++ -shared -o test.so -fPIC so_test.cpp
[root@localhost so_test]# nm -D test.so
00000000000006b5 T add
0000000000201028 B __bss_start
w __cxa_finalize
0000000000201028 D _edata
0000000000201030 B _end
00000000000006e0 T _fini
w __gmon_start__
0000000000000580 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
00000000000006c9 T sub
如果只想导出add和sub,怎么办呢?可以采用下面的方法。增加一个export.map文件
{
global:
add;
sub;
local:
*;
};
然后 编译 g++ -shared -o test.so -fPIC -Wl,--version-script,./export.map
查看。 这样就达到了控制函数输出的目的
[root@localhost so_test]# g++ -shared -o test.so -fPIC -Wl,--version-script,./export.map so_test.cpp
[root@localhost so_test]# nm -D test.so
00000000000005f5 T add
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
0000000000000609 T sub