Linux命令把共享库export,Linux 下 GCC 编译共享库控制导出函数的方法

通过一些实际项目的开发,发现这样一个现象,在 Windows 下可以通过指定 __declspec(dllexport) 定义来控制 DLL(动态链接库)中哪些函数可以导出,暴露给其他程序链接使用,哪些函数是 DLL 内部自己使用;而在 Linux 下不存在 dllexport 这样的指示字,默认情况下 GCC 编译 SO(共享库)时把代码中的所有函数都导出了,那么如何实现 Windows 下的那种效果,由我们自己来控制共享库导出函数呢?

其实在 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 参数不会对该函数起作用。所以,针对 GCC 设置了 -fvisibility=hidden 参数之后,只有设置了 __attribute__((visibility("default"))) 属性的函数才是对外可见的,如此则效果等同于 Visual Studio 下的 __declspec(dllexport) 定义。比如:

extern int foo(int a, int b, int c) __attribute__((visibility("default")));

这样声明即可。

在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse 在CentOS 6.4中编译安装GCC 4.8.1 + GDB 7.6.1 + Eclipse

GCC 的详细介绍:请点这里

GCC 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值