ios 符號隱藏

參考
Minimizing Your Exported Symbols

Dynamic Library Programming Topics

符號表查看命令

nm -gm  xxx

Using GCC 4.0 to Mark Symbol Visibility

通過 Compiler Flags 來整體控制

-fvisibility=hidden //默認沒有標記的,都是隱藏, 有標記的,以標記為主
-fvisibility=default //默認沒有標記的,都是暴露的, 有標記的,以標記為主
-fvisibility-inlines-hidden // forcing all inline functions to be hidden

通過 Visibility Attributes 來單獨控制

如果單獨加了控制,忽略編譯整體的控制,以單獨的標記為主

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

通過 Pragmas 來進行塊級別的控制

void f() { }
 
#pragma GCC visibility push(default)
void g() { }
void h() { }
#pragma GCC visibility pop

隱藏動態庫里面的內部符號

遇到的問題:

開發動態庫的時候,即使給編譯器添加了 -fvisibility=hidden, 里面使用的靜態庫的符號也被導出了, 希望隱藏使用的靜態庫的符號。

解決辦法:

指定需要導出的符號文件 -exported_symbols_list, 其他的符號都會變成內部符號。

也可以指定需要隱藏的符號,放入文件中,傳遞給 鏈接器 -unexported_symbols_list

設置路徑

  1. xcode 設置路徑路徑 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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容