在MAC OS X中将静态库的符号隐藏在动态库中? [英] Hiding the symbols of a static library in a dynamic library in MAC OS X?

查看:1234
本文介绍了在MAC OS X中将静态库的符号隐藏在动态库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MAC OS X中使用静态库(例如boost.a)和一些* .o文件来创建动态库(例如:libdynamic.dylib)。

我可以隐藏* .o文件中的符号,因为我通过-fvisibility = hidden标志创建了这些符号。但是,我无法隐藏boost.a库中的符号,因为它们已经被编译过。



有没有什么方法可以隐藏静态的符号(函数)库,在动态库?

ie,如果我有一个函数(隐藏)在myfuncs.o文件中调用boost.a文件中的函数(可见),boost.a函数是可见的我使用nm工具。



请给我建议一个解决方案。

解决方案

首先,您需要确保您声明所有要保留为externC属性((visibility(default)))的符号并检查默认情况下隐藏的符号您的Xcode项目的代码生成选项卡(我认为这是默认选中的)。

然后你需要创建一个导出的符号文件,其中包含所有你想要的符号出口(保留)。



您需要将Xcode指向此文件,方法是在Xcode项目链接器首选项中将symbols.exp添加为导出的符号文件条目。 b
$ b

确保该文件中的符号以下划线开头。您可以使用构建脚本从静态库(或原始dylib)创建导出的符号文件:

  nm -g $ BUILT_PRODUCTS_DIR / lib $ PRODUCT_NAME.dylib | ruby -ne'如果/^[0-9a-f]+.*\\s(\++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ symbols.exp 

您也可以从命令行执行此操作(替换$ BUILT_PRODUCTS_DIR / lib $ PRODUCT_NAME。在这种情况下,你的库的名称为dylib)。

这会在项目目录中创建一个导出的符号文件symbols.exp。您可以使用此符号文件从您的dylib中去除所有非必需符号,如下所示:

  strip -u -r -s symbols.exp libXYZ.dylib 

也可以把这个放在最后在你的项目中运行脚本,如下所示:

  strip -u -r -s symbols.exp $ BUILT_PRODUCTS_DIR / lib $ PRODUCT_NAME.dylib 

如果您在dylib项目中使用此脚本,请确保将symbols.exp文件也可以禁用它(点击其名称旁边的复选框),以便Xcode可以找到该文件。






在Xcode 5下,strip命令会像下面显示的那样抱怨,但该命令似乎正常工作:


/Applications/Xcode.app/内容/开发人员/工具链/ OSX10.9.xctoolchain / usr / bin / strip:不再支持从最终链接中移除全局符号。在构建时使用链接时使用-exported_symbols_list:/path/to/libYourlib.dylib

正如警告所述,使用 -exported_symbols_list 选项(或者Xcode的 Exported Symbols File 设置)允许您精确控制哪些符号将通过排除任何不在您指定的文件。


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.

解决方案

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:

nm -g $BUILT_PRODUCTS_DIR/lib$PRODUCT_NAME.dylib | ruby -ne 'if /^[0-9a-f]+.*\s(\S+)$/.match($_) then print $1,"\n" end' > symbols.exp

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:

strip -u -r -s symbols.exp libXYZ.dylib 

It might be a good idea to also put this at the end of the run script in your project as well, like so:

strip -u -r -s symbols.exp $BUILT_PRODUCTS_DIR/lib$PRODUCT_NAME.dylib 

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:

/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.9.xctoolchain/usr/bin/strip: removing global symbols from a final linked no longer supported. Use -exported_symbols_list at link time when building: /path/to/libYourlib.dylib

As the warning states, using the -exported_symbols_list option (or Xcode's Exported Symbols File setting) allows you to precisely control which symbols will be exported by excluding anything not in the file you specify.

这篇关于在MAC OS X中将静态库的符号隐藏在动态库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆