我是Linux编程的新手,我正在尝试使用BFD库。这是我正在尝试编译的当前代码:
#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):在功能上
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/buildd/binutils-2.21.53.20110810/builddir-single/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-single/bfd/../../bfd/elflink.c:6048: 未定义引用lbasename' undefined reference to
_ sch_istable'cols2:ld返回1退出 状态make: * [all]错误1
还有很多错误,您可以查看here。我确信有一个简单的解释,但它让我感到难过。
总结到目前为止我所做的事情:
答案 0 :(得分:10)
如果你使用ubuntu install binutils-dev
sudo apt-get install binutils-dev
答案 1 :(得分: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
答案 2 :(得分:2)
gcc -v main.c -o blah /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz
看起来libbfd需要来自libiberty,dl和z的功能 - 今天在opensuse 13.1 x86_64上使用类似的琐碎测试应用程序。