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

你可能感兴趣的:(ios 符号隐藏)