程式語言 - GNU - CFLAGS -fvisibility=hidden



參考資訊:
https://gcc.gnu.org/wiki/Visibility

main.c

1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
void test(void);
 
int main(int argc, char *argv[])
{
    test();
    return 0;
}

test.c

1
2
3
4
5
#include <stdio.h>
 
void test(void)
{
}

編譯

$ gcc test.c -fPIC -shared -o test.so -fvisibility=hidden
$ gcc main.c test.so 
    /usr/bin/ld: /tmp/cctmpXxQ.o: in function `main':
    main.c:(.text+0x10): undefined reference to `test'
    collect2: error: ld returned 1 exit status

$ gcc test.c -fPIC -shared -o test.so -fvisibility=default
$ gcc main.c test.so 

P.S. -fvisibility用來決定是否隱藏Shared Library的Symbol