在 Linux 上使用 bfd.h 编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8003763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile Errors using bfd.h on Linux
提问by Mike Kwan
I am new to Linux programming and am attempting to use the BFD library. This is the current code I'm trying to compile:
我是 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;
}
I am using the following command line for compiling:
我正在使用以下命令行进行编译:
gcc readInfo.c /usr/lib/libbfd.a -o readInfo
And am getting the following errors:
并收到以下错误:
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/buildd/binutils-2.21.53.20110810/builddir-single/bfd/../../bfd/elflink.c:4892: undefined reference to_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: undefined reference tolbasename' undefined reference to
_sch_istable' collect2: ld returned 1 exit status make: *[all] Error 1
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/elf48.c:60 :对lbasename' undefined reference to
_sch_istable' collect2 的未定义引用:ld 返回 1 退出状态 make:*[all] 错误 1
There are a lot more lines of errors, which you can view here. I'm sure there is a simple explanation for this but it has got me stumped for a while.
还有更多的错误行,您可以在此处查看。我确信对此有一个简单的解释,但它让我难住了一段时间。
To summarise what I have done so far:
总结一下我到目前为止所做的:
- Installed clean build of Ubuntu
- Installed binutils-dev package
- 安装干净的 Ubuntu
- 安装 binutils-dev 包
采纳答案by sarnold
Do you need to statically link your program?
你需要静态链接你的程序吗?
It compiles and runs without error if you dynamically link it instead:
如果您动态链接它,它会编译并运行而不会出错:
gcc readInfo.c -o readInfo -lbfd
I've run into a new problem when trying to make it statically linked:
在尝试使其静态链接时,我遇到了一个新问题:
$ 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
回答by Amit Chaudhuri
gcc -v main.c -o blah /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz
gcc -v main.c -o 等等 /usr/lib64/libbfd.a /usr/lib64/libiberty.a -ldl -lz
Looks like libbfd requires features from libiberty, dl and z - this on opensuse 13.1 x86_64 today with similar trivial test app.
看起来 libbfd 需要来自 libiberty、dl 和 z 的功能 - 今天在 opensuse 13.1 x86_64 上使用类似的简单测试应用程序。
回答by bitfree
if you use ubuntu install binutils-dev
如果你使用 ubuntu install binutils-dev
sudo apt-get install binutils-dev