-fvisibility=hidden的用法

man gcc:

    -fvisibility=[default|internal|hidden|protected]
           Set the default ELF image symbol visibility to the specified option---all symbols are marked with this unless overridden within the code.  Using this
           feature can very substantially improve linking and load times of shared object libraries, produce more optimized code, provide near-perfect API export
           and prevent symbol clashes.  It is strongly recommended that you use this in any shared objects you distribute.

           Despite the nomenclature, default always means public; i.e., available to be linked against from outside the shared object.  protected and internal are
           pretty useless in real-world usage so the only other commonly used option is hidden.  The default if -fvisibility isn't specified is default, i.e., make
           every symbol public.

           A good explanation of the benefits offered by ensuring ELF symbols have the correct visibility is given by "How To Write Shared Libraries" by Ulrich
           Drepper (which can be found at <http://www.akkadia.org/drepper/>)---however 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.  

example code:

foo.c:

void foo(void)
{

}
gcc -shared -fpic -g -fvisibility=hidden  foo.c -o libfoo.so

test.c

void foo();

int main(void)
{
	foo();
	return 0;
}

gcc test.c  -L. -lfoo
/tmp/cciIvtvi.o: In function `main':
test.c:(.text+0x12): undefined reference to `foo'
collect2: error: ld returned 1 exit status

修改 foo.c如下:

void __attribute__((visibility("default"))) foo(void)
{

}

然后重新编动态库, link通过。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值