在 MAC OS X 中将静态库的符号隐藏在动态库中?
我正在使用静态库(例如:boost.a)和一些 *.o 文件在 MAC OS X 中创建动态库(例如:libdynamic.dylib)。
我能够隐藏 *.o 文件中的符号,因为我通过 -fvisibility=hidden 标志创建了它们。但是,我无法隐藏 boost.a 库中的符号,因为它们已经被编译了。
有没有办法在动态库中隐藏静态库的符号(函数)?
即,如果我在 myfuncs.o 文件中有一个函数(隐藏)调用 boost.a 文件中的函数(可见),则当我使用“nm tool”时,boost.a 函数是可见的。
请给我建议一个解决方案。
I am using a static library (eg: boost.a) and some *.o files to create a dynamic library (Eg: libdynamic.dylib) in MAC OS X.
I am able to hide the symbols from the *.o files since I created those by -fvisibility=hidden flag. But, I can't hide the symbols from boost.a library since they have been compiled already.
Is there any way to hide the symbols (functions) of the static library, in the dynamic library ?
i.e., If I have a function (hidden) in myfuncs.o file which calls the functions(visible) in boost.a file, the boost.a functions are visible when I use "nm tool".
Please Suggest me a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要确保将要保留的所有符号声明为
extern "C" __attribute__((visibility("default")))
并在代码生成中选中“默认隐藏的符号” Xcode 项目的选项卡(我认为默认情况下会选中此选项)。然后,您需要创建一个导出的符号文件,其中包含要导出(保留)的所有符号。
您需要通过在 Xcode 项目链接器首选项中添加“symbols.exp”作为“导出的符号文件”条目来将 Xcode 指向此文件。
确保该文件中的符号以下划线开头。您可以使用构建脚本从静态库(或原始 dylib)创建导出的符号文件:
您也可以从命令行执行此操作(在本例中将 $BUILT_PRODUCTS_DIR/lib$PRODUCT_NAME.dylib 替换为您的库的名称)。
这将在您的项目目录中创建一个导出的符号文件“symbols.exp”。然后,您可以使用此符号文件从 dylib 中删除所有不重要的符号,如下所示:
将其也放在项目中运行脚本的末尾可能是一个好主意,如下所示:
如果您使用此脚本在您的 dylib 项目中,确保将symbols.exp 文件也添加到您的项目中,但禁用它(单击其名称旁边的复选框),以便 Xcode 可以找到该文件。
在 Xcode 5 下,strip 命令会出现如下所示的错误,尽管该命令似乎工作正常:
正如警告所述,使用
-exported_symbols_list
选项(或 Xcode 的Exported Symbols File
设置)允许您可以通过排除指定文件中未包含的任何内容来精确控制要导出的符号。First you need to make sure you declare all symbols that you want to keep as
extern "C" __attribute__((visibility("default")))
and check "symbols hidden by default" in the code generation tab of your Xcode project (I think this is checked by default).Then you need to create an exported symbols file that contains all the symbols that you want to export (keep).
You need to point Xcode to this file by adding "symbols.exp" as an "exported symbols file" entry in the Xcode project linker prefs.
Make sure that the symbols in that file start with an underscore. You can create an exported symbols file from your static lib (or the raw dylib) using the build script:
You can also do this from the command line (replace $BUILT_PRODUCTS_DIR/lib$PRODUCT_NAME.dylib by the name of your library in this case).
This will create an exported symbols file "symbols.exp" in your project directory. You can then use this symbols file to strip all nonessential symbols from your dylib, like so:
It might be a good idea to also put this at the end of the run script in your project as well, like so:
If you use this script in your dylib project make sure you add the symbols.exp file to your project as well but disable it (click the checkbox next to its name), so Xcode can find the file.
Under Xcode 5 the strip command will complain as shown below, though the command appears to work correctly:
As the warning states, using the
-exported_symbols_list
option (or Xcode'sExported Symbols File
setting) allows you to precisely control which symbols will be exported by excluding anything not in the file you specify.