ios 符號(hào)隱藏

參考
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è)置路徑

  1. xcode 設(shè)置路徑路徑 target - build settings - exported symbols files

  2. 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容