GCC:列出静态链接库

时间:2014-12-25 17:52:54

标签: c++ gcc linker ld static-linking

这就是问题所在。基本上我的问题是我正在尝试使用选项-fsanitize=address -static-libasan为ARM编译应用程序(用于调试错误指针的酷事等)。这直接编译为可执行文件,但不适用于从目标文件进行编译。我发现静态库是libasan.a;无论如何,当我试图链接目标文件中的所有内容时,它会在«pthread»中找到一堆未定义的引用,以及一些我甚至无法找到的lib-owner的函数。

所以,我只想用-fsanitize=address -static-libasan编译一个文件,看看哪些库是静态链接的,以便我以后可以从一个目标文件中进行。

1 个答案:

答案 0 :(得分:1)

可执行文件不存储从中提取目标文件的任何静态库的跟踪。但是,大多数情况下,这些库在链接时是明确指定的。编译器只引入一些标准的静态库。对于gcc,您可以看到各种信息,包括使用-v选项传递给链接器的实际命令行(似乎某些实现想要使用--verbose和链接器-Wl,--verbose )。此输出应显示所有隐式链接的库,如-lgcc-lstdc++

请注意,您应该使用特定于语言的前端和编译时相同的编译器标志进行链接。您的问题说明听起来好像在编译时使用-pthreadg++但是您尝试使用gcc进行链接而没有-pthread标记(有一些不同的线程相关标记;从描述中不可能分辨出哪一个被使用了。

只是为了处理侮辱,这是在我的计算机上使用-v选项时的示例输出:

ThreadGard:stackoverflow kuehl$ cat hello.cpp 
#include <iostream>

int main()
{
  std::cout << "hello, world\n";
  return 0;
}
ThreadGard:stackoverflow kuehl$ g++ -v -static hello.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin13.4.0
Configured with: ../gcc-4.9.2/configure --prefix=/opt/gcc-4.9.2 --with-gmp=/opt/gcc-infrastructure --with-mpfr=/opt/gcc-infrastructure --enable-decimal-float=bid --enable-lto --enable-languages=c,c++
Thread model: posix
gcc version 4.9.2 (GCC) 
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
 /opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/cc1plus -quiet -v -D__STATIC__ hello.cpp -quiet -dumpbase hello.cpp -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase hello -version -o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccrt7fXS.s
GNU C++ (GCC) version 4.9.2 (x86_64-apple-darwin13.4.0)
    compiled by GNU C version 4.9.2, GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../x86_64-apple-darwin13.4.0/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2
 /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2/x86_64-apple-darwin13.4.0
 /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2/backward
 /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/include
 /usr/local/include
 /opt/gcc-4.9.2/include
 /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/include-fixed
 /usr/include
 /System/Library/Frameworks
 /Library/Frameworks
End of search list.
GNU C++ (GCC) version 4.9.2 (x86_64-apple-darwin13.4.0)
    compiled by GNU C version 4.9.2, GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: fea0def5b69cf8ae349b2f1faf4b1d23
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
 as -arch x86_64 -force_cpusubtype_ALL -static -o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccrt7fXS.s
COMPILER_PATH=/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/
LIBRARY_PATH=/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../:/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
 /opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/collect2 -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2 -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../.. /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v
collect2 version 4.9.2
/usr/bin/ld -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2 -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../.. /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v
@(#)PROGRAM:ld  PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
Library search paths:
    /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2
    /opt/gcc-4.9.2/lib
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/
ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status

并未安装所有静态库以使其实际链接。但是,除了这一点之外,输出清楚地列出了链接器调用:

  

/ usr / bin / ld -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L / opt / gcc-4.9.2 / lib / gcc / x86_64- apple-darwin13.4.0 / 4.9.2 -L / opt / gcc-4.9.2 / lib / gcc / x86_64-apple-darwin13.4.0 / 4.9.2 /../../ .. / var / folders / b_ /64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v

(加上一些重点使读者更容易 - 似乎是必要的)