C++ 获取可执行文件中使用的静态库列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1124571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:28  来源:igfitidea点击:

Get list of static libraries used in an executable

c++cunixstaticlinker

提问by Saurabh

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

由于ldd仅列出动态库,有没有办法提取有关用于创建可执行文件的静态库的信息?

回答by DrAl

ldd <exe filename>shows dynamically linked libraries

ldd <exe filename>显示动态链接库

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

nm <exe filename>显示文件中的符号。

To see which symbols come from static libraries requires running nmagainst 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>.

要查看哪些符号来自静态库,需要nm针对这些库运行以获取其中的符号(函数等)列表,然后将它们与来自nm <exe filename>.

You compare lists with the commcommand. See man commfor details.

您可以将列表与comm命令进行比较。详情请参阅man comm

This was taken from this forum here.

这是从这个论坛here中获取的

回答by Saurabh

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.

不,在链接过程中会丢弃库的名称。但是,如果您的可执行文件包含调试信息(即它是用 -g 标志编译的),您可能能够从中获取信息。

回答by zhangxin

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.

例如g++ -Xlinker -Map=a.map main.c,检查映射文件以获取链接的静态库信息。

回答by Basile Starynkevitch

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

无法获取某些ELF可执行文件中的静态库列表。

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.oof libfoo.aare linked as if object file foo2.owas linked into the executable (provided some symbol defined in foo2is needed, i.e. is referenced somewhere).

因为对于链接器,静态库只是用作“惰性”成员集。因此生成的 ELF 可执行文件将只包含链接它所需的成员。所以像foo2.oof 之类的成员libfoo.a被链接起来,就好像目标文件foo2.o被链接到可执行文件中一样(假设foo2需要定义一些符号,即在某处引用)。

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

当然,在某些 ELF 可执行文件上使用nm, or objdump, or readelf, orstrings可能会提供一些关于其中包含哪些目标文件(包括来自静态库的目标文件)的提示,因为您会看到在这些静态库(的成员)中定义的符号(或在其中使用的文字字符串)。

回答by Goz

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.

除非给定的编译器在二进制文件中存储某种元数据,否则不会。静态库是直接编译成二进制的代码。