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

后端 未结 5 935
灰色年华
灰色年华 2021-01-30 17:29

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

  • My main app
  • Several app-specific libraries (libapp1, libapp2, etc...)
  • Several \"thi
相关标签:
5条回答
  • 2021-01-30 18:06

    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.

    0 讨论(0)
  • 2021-01-30 18:11

    I'll answer your second question first. You can simply use the -H or -M flag to see all (including system) headers processed in the compilation. gcc -H main.c should do the trick. Seeing which headers are included will actually get you on the right track to finding which static libraries were linked in.

    You could use objdump on your final object (or readelf on your final binary) to get the names of all the functions in there. You'd then have to go find the libraries from which the functions were pulled in, but that's a bit cumbersome. You'd definitely have to make a script to minimize the pain.

    Someone else mentioned using gcc <stuff> -Wl,-verbose which simply passes the -verbose flag to the linker. That's a perfect way to get a list of shared libraries (.so files), but you said yours are static, so that isn't the way to go in this case.

    Good luck!

    0 讨论(0)
  • 2021-01-30 18:14

    As far as I know, not much information about static libraries is preserved when linking (since the linker just sees that library as a collection of *.o objects anyway).

    If you find the make command that links the final executable and add a -v flag, g++ will show you exactly how it calls the ld command. This should include all necessary static libraries, including libraries used by other libraries, or otherwise the link step would fail. But it might also include extra libraries that aren't actually used.

    Another possibly useful thing is that, at least on Linux, objects and executables usually store names of the source code files from which they were created. (Filename only, no path.) Try

    objdump -t executable | grep '*ABS*'
    
    0 讨论(0)
  • 2021-01-30 18:28

    For direct dependencies;

    ldd <app>
    

    Indirect/All dependencies;

    ldd -r <app>
    
    0 讨论(0)
  • 2021-01-30 18:29

    Try to use ldd + your filename, this will list the libs.

    0 讨论(0)
提交回复
热议问题