Get list of static libraries used in an executable

C++CUnixStaticLinker

C++ Problem Overview


Since ldd lists only the dynamic libraries, is there a way to extract the information about the static libraries used to create the executable?

C++ Solutions


Solution 1 - C++

ldd <exe filename> shows dynamically linked libraries

nm <exe filename> shows the symbols in the file.

To see which symbols come from static libraries requires running nm against those libraries to get a list of the symbols (functions, etc.) in them, then comparing them to what your list of symbols from nm <exe filename>.

You compare lists with the comm command. See man comm for details.

This was taken from this forum here.

Solution 2 - C++

No, the names of the libraries are discarded during the linking process. However, if your executable contains debug information (i.e. it was compiled with the -g flag), you may be able to get information from that.

Solution 3 - C++

If you have the source code and don't want to go through all the code for this, you can generate map file while compiling to know which static libraries are linked.

For example g++ -Xlinker -Map=a.map main.c, check the map file for linked static library information.

Solution 4 - C++

There is no way to get the list of static libraries inside some ELF executable.

Because for the linker, a static library is just used as a "lazy" set of members. So the resulting ELF executable would only contain the members needed to link it. So members like foo2.o of libfoo.a are linked as if object file foo2.o was linked into the executable (provided some symbol defined in foo2 is needed, i.e. is referenced somewhere).

Of course, using nm, or objdump, or readelf, or strings on some ELF executable may give some hints about what object files (including those coming from static libraries) are inside it, because you'll see symbols defined in (members of) those static libraries (or literal strings used inside them).

Solution 5 - C++

Unless a given compiler stores some sort of meta data inside the binary then, no. A static library is code that is directly compiled into the binary.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaurabhView Question on Stackoverflow
Solution 1 - C++DrAlView Answer on Stackoverflow
Solution 2 - C++anonView Answer on Stackoverflow
Solution 3 - C++xzhangxaView Answer on Stackoverflow
Solution 4 - C++Basile StarynkevitchView Answer on Stackoverflow
Solution 5 - C++GozView Answer on Stackoverflow