Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a report of ALL the libraries linked when building my C++ executable (gcc)? (including statically linked)

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

  • My main app
  • Several app-specific libraries (libapp1, libapp2, etc...)
  • Several "third party" libraries (most "third partis are just other teams in the company") linked from both the main app, from the app-specific libappX libraries, and from other 3rd part libraries - e.g. 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> 

QUESTIONs:

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

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

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.

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.

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) 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.

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

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

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

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

like image 272
DVK Avatar asked Feb 07 '11 18:02

DVK


People also ask

What is the difference between C executable and C library?

A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable. You would be familiar about the compilation of a C file.

What is static library gcc?

A static library is basically a set of object files that were copied into a single file. This single file is the static library. The static file is created with the archiver (ar). First, calc_mean.c is turned into an object file: gcc -c calc_mean.c -o calc_mean.o.

Is LIBC a dynamic library?

However libc has not been linked in statically, only dynamically, so it is another failed attempt.


2 Answers

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 

Here is example output:

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 

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

Update 2: -Wl,--trace does not display libraries included indirectly. Example: you link with libA, and libA was linked with libB. If you want to see that libB is needed too, you must use -Wl,--verbose.

like image 175
Daniel Frużyński Avatar answered Oct 12 '22 09:10

Daniel Frużyński


For direct dependencies;

ldd <app> 

Indirect/All dependencies;

ldd -r <app> 
like image 44
ismail Avatar answered Oct 12 '22 11:10

ismail