參考
Minimizing Your Exported Symbols
Dynamic Library Programming Topics
符號(hào)表查看命令
nm -gm xxx
Using GCC 4.0 to Mark Symbol Visibility
通過(guò) Compiler Flags 來(lái)整體控制
-fvisibility=hidden //默認(rèn)沒(méi)有標(biāo)記的,都是隱藏, 有標(biāo)記的,以標(biāo)記為主
-fvisibility=default //默認(rèn)沒(méi)有標(biāo)記的,都是暴露的, 有標(biāo)記的,以標(biāo)記為主
-fvisibility-inlines-hidden // forcing all inline functions to be hidden
通過(guò) Visibility Attributes 來(lái)單獨(dú)控制
如果單獨(dú)加了控制,忽略編譯整體的控制,以單獨(dú)的標(biāo)記為主
__attribute__((visibility("default"))) void MyFunction1() {}
__attribute__((visibility("hidden"))) void MyFunction2() {}
Visibility attributes override the value specified with the -fvisibility flag at compile-time. Thus, adding the default visibility attribute causes a symbol to be exported in all cases, whereas adding the hidden visibility attribute hides it.
Visibility attributes may be applied to functions, variables, templates, and C++ classes. If a class is marked as hidden, all of its member functions, static member variables, and compiler-generated metadata, such as virtual function tables and RTTI information, are also hidden.
通過(guò) Pragmas 來(lái)進(jìn)行塊級(jí)別的控制
void f() { }
#pragma GCC visibility push(default)
void g() { }
void h() { }
#pragma GCC visibility pop
隱藏動(dòng)態(tài)庫(kù)里面的內(nèi)部符號(hào)
遇到的問(wèn)題:
開(kāi)發(fā)動(dòng)態(tài)庫(kù)的時(shí)候,即使給編譯器添加了 -fvisibility=hidden, 里面使用的靜態(tài)庫(kù)的符號(hào)也被導(dǎo)出了, 希望隱藏使用的靜態(tài)庫(kù)的符號(hào)。
解決辦法:
指定需要導(dǎo)出的符號(hào)文件 -exported_symbols_list, 其他的符號(hào)都會(huì)變成內(nèi)部符號(hào)。
也可以指定需要隱藏的符號(hào),放入文件中,傳遞給 鏈接器 -unexported_symbols_list
設(shè)置路徑
xcode 設(shè)置路徑路徑
target - build settings - exported symbols files-
cocoapods,podspec 指定
s.pod_target_xcconfig = { "EXPORTED_SYMBOLS_FILE" => "$(PODS_TARGET_SRCROOT)/xxx/export_symbols_ios" }
注意
-exported_symbols_list can not work static lib, but can take effect on object file
參考: https://stackoverflow.com/questions/6894214/how-to-create-static-library-for-ios-without-making-all-symbols-public