2014-12-25 202 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'是靜態鏈接的。 –