Reputation: 1243
I want to cut down library dependencies on some C++ executables I'm compiling for Linux with GCC. There is a list of probably 40 static libraries that being linked. I want to determine which ones are unnecessary, and I would rather not try removing them one at a time to find out.
Is there an option in GCC to make it warn about libraries that are linked but don't resolve any symbols?
Are there any Linux tools available that will help me out?
To be clearn, I'm not concerned with unused code being linked into the executable. Rather, I'm concerned with the unnecessary build dependencies. I'd like to cut down my build times.
Upvotes: 3
Views: 1116
Reputation: 32514
Building on top of the information provided by @ThomasMatthews in a comment to the question:
Run the linker with the -M
option and pipe its output to the following script:
get_used_libs
#!/bin/bash
sed -e '/^Discarded input sections$/,$ d' \
-e '/^Archive member included to satisfy reference by file (symbol)$/ d' \
-e '/^As-needed library included to satisfy reference by file (symbol)$/ d' \
-e '/^Discarded input sections$/ d' \
-e '/^$/ d; /^\s/ d; s/\s\+.\+//; s/(.\+//' \
| sort -u
It will return the list of the libraries that were necessary for your program to link.
Disclaimer: The script was only tested on output from GNU ld (GNU Binutils for Ubuntu) 2.25.1
on a toy program.
Upvotes: 5
Reputation: 16146
While not as convenient as the other answer, you can use nm
to list the required (U
) and exported (anything else, but beware of [VvWw]
) symbols for any object or archive.
A little preprocessing with grep and/or sed might make this more helpful for some use cases.
Upvotes: 1