Avoid linking unused characters when linking to static libraries

I am using apple gcc to compile dylib, which I am going to redistribute. For various reasons, I use some libraries, say libz , to make it simple.

Since this library is not usually found on a Mac system, I want a static link to use characters in dylib by passing the path to .a-file to simplify the deployment.

Now the linker is linked in all the characters from lib to the resulting dylib, although I am only referring to a subset. In linux, I never encountered this problem, the linker happily discards all characters without links and creates a very thin executable, so this should be possible. The dylib file that I have is now ~ 10 times larger than needed.

I tried playing with the -dead_code flag, but to no avail. Maybe I just don't get it?

Does anyone know how to solve this?

+3
source share
1 answer

Try -Wl,--gc-sections .

Regarding -dead_strip (which you probably meant by -dead_code ):

Before turning on -dead_strip, your project version must first be β€œported” to work with dead stripping code. This will include changing from -gused (the default value for -g) to -gfull and recompiling all the object files associated with your program with the new compiler from the Mac OS X release in June 2004. Also, if your building is an executable that loads plugins that use characters from the executable, you will need to make sure the characters used by the plugins are not without (using ((used)) or the -exported_symbols_list parameter). If you are using an export list and building a shared library or executable file that will be used with ld (1) -bundle_loader, you need to include characters to exclude frame information in the export list for exported C ++ characters. These characters end in .eh and can be seen with the nm (1) tool.

and :

To enable the removal of security code from the command line, go to the -dead_strip option to ld. You should also go -gfull for GCC to create a complete set of debugging symbols for your code. Linker uses this additional debugging information to dead band executable.

Hope this helps.

All of the content of this answer was located in the first few Google search results for "unused Apple static link characters ld." :)

+5
source

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


All Articles