Hiding static library characters in a dynamic library in MAC OS X?

I use a static library (ex: boost.a) and some * .o files to create a dynamic library (ex: libdynamic.dylib) on MAC OS X.

I can hide characters from * .o files since I created them using -fvisibility = hidden flag. But I can not hide the characters from the boost.a library, since they are already compiled.

Is there a way to hide the symbols (functions) of a static library in a dynamic library?
those. if I have a function (hidden) in the myfuncs.o file that calls functions (visible) in the boost.a file, boost.a functions are visible when I use the nm tool.

Please offer me a solution.

+6
source share
1 answer

First you need to make sure that you declare all the characters that you want to save as an extern โ€œCโ€ attribute ((visibility (โ€œdefaultโ€))) and mark the โ€œcharacters hidden by defaultโ€ in the code generation tab of your Xcode project (by -My, this is checked by default).

Then you need to create an exported symbol file containing all the characters you want to export (save).

You need to specify Xcode in this file by adding "symbol.exp" as the entry for the "exported symbol file" in the Xcode project linker prefix.

Make sure that the characters in this file begin with an underscore. You can create an exported symbol file from your static lib (or 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 (in this case, replace $ BUILT_PRODUCTS_DIR / lib $ PRODUCT_NAME.dylib with the name of your library).

This will create the exported symbol file "symbols.exp" in your project directory. You can then use this character file to remove all non-essential characters from your dylib, for example:

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

It might be nice to put this at the end of the script run in your project, for example:

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

If you use this script in your dylib project, be sure to add the symbol.exp file to your project, but disable it (check the box next to its name), so Xcode can find the file.


In Xcode 5, the strip command will complain, as shown below, although the command works correctly:

/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.9.xctoolchain/usr/bin/strip: removing global characters from the final link is no longer supported. Use -exported_symbols_list at build time when creating: /path/to/libYourlib.dylib

As warning states, using the -exported_symbols_list parameter (or the Xcode Exported Symbols File setting) allows you to precisely control which characters will be exported, excluding everything that is not specified in the specified file.

+4
source

Source: https://habr.com/ru/post/892987/


All Articles