Compile errors using bfd.h on Linux

I am new to Linux programming and am trying to use the BFD library. This is the current code I'm trying to compile:

#include <bfd.h> #include <stdlib.h> bfd *abfd; int main() { bfd_init(); abfd = bfd_openr("/home/mike/Desktop/testFunc/testProg", NULL); return 0; } 

I use the following command line to compile:

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

And I get the following errors:

gcc readInfo.c / usr / lib / libbfd.a -o readInfo /usr/lib/libbfd.a(elflink.o): 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 link 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 link to lbasename' undefined reference to _sch_istable' collect2: ld returned 1 output status make: * [all] Error 1

There are many more error lines here that you can view here here . I am sure there is a simple explanation for this, but for a while I am a little puzzled.

To summarize what I have done so far:

  • Ubuntu Clean Build Installed
  • Installed binutils-dev package
+6
source share
3 answers

Do you need to statically link your program?

It compiles and starts without errors if you dynamically link it:

 gcc readInfo.c -o readInfo -lbfd 

I ran into a new problem while 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 
+3
source

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

It seems that libbfd requires functions from libiberty, dl and z - this now opens 13.1 x86_64 with a similar trivial test application.

+2
source

if you use ubuntu install binutils-dev

 sudo apt-get install binutils-dev 
+2
source

Source: https://habr.com/ru/post/900745/


All Articles