在构建我的C ++可执行文件(gcc)时,我可以获得所有链接的库的报告吗? (包括静态链接)

在构建我的C ++可执行文件(gcc)时,我可以获得所有链接的库的报告吗? (包括静态链接)

问题描述:

我有一个我继承的C ++应用程序,它包括:

I have a C++ application that I inherited, which consists of:


  • 我的主要应用程序

  • 几个应用程式特定的程式库(libapp1,libapp2等)

  • 几个「第三方」程式库(大多数「第三方只是公司其他团队」)无论是主应用程序,从应用程序特定的libappX库,以及从其他第三方库 - 例如libext1,libext2,etc ...

换句话说,我的代码如下所示:

In other words, my code looks like this:

// main.C
#include <app1/a1l1.H>
#include <app2/a2l1.H>
#include <ext1/e1l1.H>

// app1/a1l1.H
#include <app1/a1l2.H>
#include <ext2/e2l1.H>

// app2/a2l1.H
#include <ext2/e2l2.H>

// ext1/e1l1.H
#include <ext3/e3l1.H>

// ext3/e3l1.H
#include <ext4/e4l1.H>

问题:

1)我如何知道哪些库已经链接到最终的可执行文件? 这必须包括静态链接

1) How can I tell which libraries have been linked into the final executable? This must include statically linked ones

换句话说,我想要回答app1,app2,ext1,ext2,ext3,ext4

In other words, I want an answer of "app1, app2, ext1, ext2, ext3, ext4"

理想情况下,答案将从可执行文件本身(我有一个调试版本的内置的情况下,它使更可能)。如果这是不可能的,我想知道是否有一个简单的代码分析工具(在gcc本身内部的东西)提供分析。

Ideally, the answer would be available from the executable itself (I have a debug version of it built in case it makes it more possible). If that's impossible, i'd like to know if there's a simple code analysis tool (iedeally something within gcc itself) to provide that analysis.

请注意,目标文件对于外部库已经构建,所以查看构建日志以查看链接,我担心ext4将不会显示在日志中,因为我们不会建立ext3库已经预先内置。

Please note that the object files for external libraries are already built, so looking at the build logs to see what was linked, I'm worried that "ext4" won't show up in the log since we won't be building "ext3" library that is already pre-built.

注意:使用DEPS设置为yes来运行nmake,重建所有的不是一个选项。但我可以访问外部库的完整的源代码。

NOTE: running "nmake" with DEPS set to yes to rebuild all the is NOT an option. But i DO have access to the full source code for external libraries.

2)一个稍微分开和不太重要的问题,我怎么能告诉所有包含文件用于我正在构建的整个源代码树。再次,理想情况下,frm已经构建的可执行文件,我有一个调试版本。

2) A slightly separate and less important question, how can i tell a list of all the include files used in the entire source tree I'm building. Again, ideally frm already-built executable, which i have a debug version of.

=================

=================

更新:为了说明,我们的库是静态链接的,因此 ldd (List Synamic Dependencies)不工作。

UPDATE: Just to clarify, our libraries are linked statically, so ldd (List Synamic Dependencies) does not work.

此外,答案可以是Solaris或Linux - 无所谓。

Also, the answer can be either for Solaris or Linux - doesn't matter.

我尝试使用 nm 但是没有列出库

I tried using nm but that doesn't list the libraries

我有类似的问题,并发现解决方案:add -Wl, - verbose选项时链接。它会将链接器切换到详细模式:

I had similar problem and found solution: add -Wl,--verbose option when linking. It will switch linker to verbose mode:

gcc -o test main.o -ltest -L. -Wl,--verbose

以下是示例输出:

GNU ld (GNU Binutils) 2.23.52.20130604
  Supported emulations:
   i386pep
   i386pe
using internal linker script:
==================================================
/* Default linker script, for normal executables */
[many lines here]
==================================================
attempt to open /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/crt0.o succeeded
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/crt0.o
attempt to open /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o succeeded
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o
attempt to open main.o succeeded
main.o
attempt to open ./libtest.dll.a failed
attempt to open ./test.dll.a failed
attempt to open ./libtest.a succeeded
(./libtest.a)test.o
[more lines here]
attempt to open /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtend.o succeeded
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtend.o

更新:您也可以使用-Wl, - trace选项-Wl, - verbose。它还会给你的库列表,但是较少冗长。

Update: You can also use -Wl,--trace option instead of -Wl,--verbose. It will also give you list of libraries, but is less verbose.