设置符号可见性
vis.c
#include <stdio.h>
#include <stdlib.h>
__attribute ((visibility("default"))) void not_hidden ()
{
printf("exported symbol\n");
}
void is_hidden ()
{
printf("hidden one\n");
}
想要做的是,第一个函数符号可以被导出,第二个被隐藏。
先编译成一个动态库,使用到属性-fvisibility
gcc -shared -fPIC -o libvis.so -fvisibility=hidden vis.c
$ readelf -s libvis.so | grep hidd
11: 00000000000006a0 19 FUNC GLOBAL DEFAULT 12 not_hidden
40: 00000000000006b3 19 FUNC LOCAL DEFAULT 12 is_hidden
55: 00000000000006a0 19 FUNC GLOBAL DEFAULT 12 not_hidden
visMain.c
int main()
{
not_hidden();
is_hidden();
return 0;
}
$ gcc -o visMain.out visMain.c -L ./ -lvis
visMain.c: In function ‘main’:
visMain.c:3:2: warning: implicit declaration of function ‘not_hidden’ [-Wimplicit-function-declaration]
not_hidden();
^
visMain.c:4:2: warning: implicit declaration of function ‘is_hidden’ [-Wimplicit-function-declaration]
is_hidden();
^
/tmp/cc1hSyEJ.o: In function `main':
visMain.c:(.text+0x14): undefined reference to `is_hidden'
collect2: error: ld returned 1 exit status
可见性为LOCAL 了,所以无法链接