在Linux上使用bfd.h编译错误

前端之家收集整理的这篇文章主要介绍了在Linux上使用bfd.h编译错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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): 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 to lbasename'
undefined reference to
_sch_istable’ collect2: ld returned 1 exit
status make: * [all] Error 1

还有很多错误,你可以查看here.我确定有一个简单的解释,但它让我难倒了一段时间.

总结到目前为止我所做的事情:

>安装了Ubuntu的干净版本
>安装binutils-dev包

解决方法

你需要静态链接你的程序吗?

如果你动态链接它,它编译并运行没有错误

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

猜你在找的Linux相关文章