2011-11-04 129 views
6

我是Linux編程新手,試圖使用BFD庫。這是當前代碼我試圖編譯:在Linux上使用bfd.h編譯錯誤

#include <bfd.h> 
#include <stdlib.h> 

bfd *abfd; 

int main() { 
    bfd_init(); 
    abfd = bfd_openr("/home/mike/Desktop/testFunc/testProg", NULL); 
    return 0; 
} 

我使用下面的命令行編譯:

gcc readInfo.c /usr/lib/libbfd.a -o readInfo 

而且正在以下錯誤:

GCC readInfo.c /usr/lib/libbfd.a -o readInfo /usr/lib/libbfd.a(elflink.o):In function elf_link_add_object_symbols': /build/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:4605: undefined reference to objalloc_free_block' /build/build d/binutils的-2.21.53.20110810/builddir單/ BFD /../../ BFD/elflink.c:4892: 未定義參照_sch_istable' /usr/lib/libbfd.a(elflink.o): In function bfd_elf_size_dynamic_sections': /build/buildd/binutils-2.21.53.20110810/builddir-單/ BFD /../../ BFD/elflink.c:6048: 未定義參照lbasename' undefined reference to _sch_istable」 collect2:LD返回1退出 狀態化妝:* [全部]錯誤1

有是更多的錯誤線,您可以查看here。我確信這有一個簡單的解釋,但它讓我陷入了一段時間。

要總結一下我迄今所做的:

  • 安裝乾淨的構建的Ubuntu
  • 安裝的binutils-dev軟件包

回答

3

你需要靜態鏈接程序?

它編譯無誤的運行,如果動態地進行鏈接,而不是:試圖使其靜態鏈接時

gcc readInfo.c -o readInfo -lbfd 

我碰到了新的問題:

$ gcc readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a -o readInfo 
/usr/bin/ld.bfd.real: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality 
    in `/usr/lib/x86_64-linux-gnu/libc.a(strcmp.o)' can not be used when making 
    an executable; recompile with -fPIE and relink with -pie 
collect2: ld returned 1 exit status 
$ gcc -fPIE readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a \ 
    -o readInfo 
/usr/bin/ld.bfd.real: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality 
    in `/usr/lib/x86_64-linux-gnu/libc.a(strcmp.o)' can not be used when making 
    an executable; recompile with -fPIE and relink with -pie 
collect2: ld returned 1 exit status 
$ gcc -fPIE -pie readInfo.c /usr/lib/libbfd.a /usr/lib/x86_64-linux-gnu/libc.a \ 
    -o readInfo 
/usr/bin/ld.bfd.real: /usr/lib/libbfd.a(opncls.o): relocation R_X86_64_32S 
    against `.rodata' can not be used when making a shared object; recompile with 
    -fPIC 
/usr/lib/libbfd.a: could not read symbols: Bad value 
collect2: ld returned 1 exit status 
$ gcc -fPIC -fPIE -pie readInfo.c /usr/lib/libbfd.a \ 
    /usr/lib/x86_64-linux-gnu/libc.a -o readInfo 
/usr/bin/ld.bfd.real: /usr/lib/libbfd.a(opncls.o): relocation R_X86_64_32S 
    against `.rodata' can not be used when making a shared object; recompile with 
    -fPIC 
/usr/lib/libbfd.a: could not read symbols: Bad value 
collect2: ld returned 1 exit status 
+0

我不知道,我在做什麼是靜態鏈接。是否有靜態鏈接會導致這些錯誤的原因? –

+0

'/ usr/lib/libbfd.so'依賴於其他動態庫的一些功能 - 所以靜態'.a'版本在鏈接最終可執行文件時還需要其他靜態庫。我無法弄清楚如何靜態構建它。 – sarnold

+0

好的。去接受這個答案,因爲它解決了眼前的問題,但如果你最終發現如何正確靜態編譯它,我會非常感激:) –

2

GCC -v main.c -o blah /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz

看起來像libbfd需要來自libiberty,dl和z的功能 - openuseuse 13.1 x86_64今天帶有simi lar瑣碎的測試應用程序。

1

,如果你使用Ubuntu安裝的binutils-dev的

sudo apt-get install binutils-dev