2014-12-25 119 views
-1

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

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

+2

_“...它说的一堆未定义的参考«pthread»...”_试图添加“-pthread”编译器和链接器选项? –

+0

@πάνταῥεῖ当然。 –

+0

如果在名词前使用“a”,则不能将名词置于复数形式。 “一个图书馆”,“一个错误的指针”,“一个目标文件” - 所有这些都是同样荒谬的。 –

回答

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非弱-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 -app-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++一个-no_compact_unwind -no_pie -lgcc_eh -lgcc -v

(有一些重点的加入使读者更容易 - 这似乎是NE子宫颈)

+0

完全没有回答。我不想从编译后的二进制文件中获取有关静态库的信息,我只是想在编译时显示一个静态链接库的选项。例如。尝试创建一个简单的«helloworld。cpp»,并用'g ++ -static-libgcc helloworld.cpp'进行编译。你如何知道静态链接的库? *(在这个例子中显然是'libgcc',但在我的问题中这是行不通的 - 我已经找到了'libasan',并且仍然有一些库似乎受到'-static-libasan'影响*)* –

+0

'-v'选项的哪一部分我没有提到? –

+0

事实上,DietmarKühl你是否自己尝试过?我从输出中看不到任何提示,即我之前评论中的一个小例子中的'libgcc'是静态链接的。 –