控制linux动态链接库(so, standard object)导出函数

参考:http://www.lampchina.net/article/htmls/201006/Mjg4MDMy.html

注意:参考文献中有一些函数是不能编译通过的。

编译环境:gcc 4.4.3

在windows中,我们可以指定__declspec(dllexport)定义来控制导出函数,在linux下,我们也有类似的控制参数。

在GCC帮助文档 -fvisibility=default|internal|hidden|protected参数下有这样一段描述:

a superior solution made possible by this option to marking things hidden when the default is public is to make the default hidden and mark things public. This is the norm with DLL's on Windows and with -fvisibility=hidden and "__attribute__ ((visibility("default")))" instead of "__declspec(dllexport)" you get almost identical semantics with identical syntax. This is a great boon to those working with cross-platform projects.

需要了解的是,在linux下,源文件中的所有函数都有一个默认的visibility属性为public,在编译命令中加入-fvisibility=hidden参数,会将所有默认的public的属性变为hidden。此时,如果对函数设置__attribute__ ((visibility("default")))参数,使特定的函数仍然按默认的public属性处理,则-fvisibility=hidden参数不会对该函数起作用。所以,设置了-fvisibility=hidden参数之后,只有设置了__attribute__ ((visibility("default")))的函数才是对外可见的,如此则效果等同于Visual Studio下的__declspec(dllexport)定义。

下面看一个例子:

此时调用以下命令编译:

可以获得test.so文件,用以下命令查看so文件属性:

得到以下结果:

可以看出,so文件中包含test2()和test3()这两个函数,并且test2()的属性为default(即可见),而test3()的属性则为hidden(不可见)。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值