Linux共享库控制符号输出

先把代码贴出来

#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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值