NotePublic/Software/Development/Standard/GCC/GCC_库导出符号控制.md

40 lines
662 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GCC 库导出符号控制
GCC 编译库的时候默认导出所有符号。可通过指定以下参数选择默认的导出方案:
```bash
-fvisibility=default|internal|hidden|protected
```
如果设置成了 hidden那么可在函数前增加
```cpp
__attribute__ ((visibility("default")))
```
属性使其外部可见,或指定其属性。如:
```cpp
/**
* @file example.c
*/
void func0(void)
{
...
}
__attribute__ ((visibility("default")))
void func1(void)
{
...
}
```
然后:
```bash
gcc -shared -o libexample.so -fvisibility=hidden example.c
```
则 void func1(void) 将被导出,而 void func0(void) 会被隐藏。