Linker not finding symbols, but library and link command look ok
I'm trying to compile a program using the bfd library. This is installed, both libbfd.so and libbfd.a exist in /usr/lib. The link command does have "-lbfd" and the main source compiles to a .o file just fine. Yet linking results in undefined reference to `bfd_openr' Readelf (and other means) verify that bfd_openr exists in the library.
So, I'm missing something... I think what I need most is a checklist: Everything needed to link a library into a program. |
can you show command line you used and output?
|
Quote:
Code:
/usr/bin/ld: cannot find -lbfd Second, check that ld is finding the "correct" version of libbfd by passing "-Wl,-verbose" to gcc (i.e. tell it to pass the "-verbose" option to the linker) and have a look at where it finds libbfd - e.g. a successful hunt for -lasound on my system looks like: Code:
... John G |
Here are the actual commands. I even put in -L/usr/lib in wild hopes that gcc was being stupid and had to be told. Same error.
gcc -L/usr/lib -g -o lsstack -Wall -m64 -lbfd -liberty lsstack.o lsstack.o: In function `get_file_symbols': /home/darenw/SW/src/lsstack/lsstack.c:703: undefined reference to `bfd_openr' (several more undef. ref. errors for other symbols follow) |
The verbose flag is a good idea, John. Here's a relevant portion of the output:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/crtbegin.o attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libbfd.so failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libbfd.a failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libbfd.so failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libbfd.a failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../lib/libbfd.so succeeded opened script file /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../lib/libbfd.so opened script file /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../lib/libbfd.so attempt to open /usr/lib/libbfd.a succeeded attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libiberty.so failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libiberty.a failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libiberty.so failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/libiberty.a failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../lib/libiberty.so failed attempt to open /usr/lib/gcc/x86_64-unknown-linux-gnu/4.4.2/../../../../lib/libiberty.a succeeded Then after some other libraries are found, the same unef. ref. errors appear. But now I notice the package manager for my distro (Arch) doesn't know about libbfd as package. It may be supplied in some other package. I am checking... |
Quote:
I wonder if this could be an problem with ordering - I didn't think it would be a problem for .o files, just for libraries included with -l, but it's worth trying. Could you try putting -lbfd after lsstack.o? |
Quote:
How dumb to forget about such a thing as ordering of libraries and .o on the command line. I guess I don't make that mistake often enough to consider it during those times when it _is_ a problem. |
btw, libbfd comes from the binutils package, and is up to date.
|
Posting the command which compile lsstack successfully
gcc -W -Wall -g -o lsstack lsstack.c -lbfd -liberty
|
All times are GMT -5. The time now is 03:15 PM. |