LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-04-2009, 01:20 PM   #1
darenw
Member
 
Registered: Mar 2009
Location: Very humid Central Florida
Distribution: ArchLinux
Posts: 30

Rep: Reputation: 1
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.
 
Old 12-04-2009, 01:25 PM   #2
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 260

Rep: Reputation: 32
can you show command line you used and output?
 
Old 12-04-2009, 02:12 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by darenw View Post
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.
The first thing I'd say is to double-check that you're not getting an error message like:

Code:
/usr/bin/ld: cannot find -lbfd
somewhere down the line.

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:
...
attempt to open /usr/lib/gcc/i486-linux-gnu/4.4.1/libasound.so failed
attempt to open /usr/lib/gcc/i486-linux-gnu/4.4.1/libasound.a failed
attempt to open /usr/lib/gcc/i486-linux-gnu/4.4.1/libasound.so failed
attempt to open /usr/lib/gcc/i486-linux-gnu/4.4.1/libasound.a failed
attempt to open /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libasound.so succeeded
...
That might tell you if ld is finding an older version of libbfd.

John G
 
Old 12-04-2009, 03:07 PM   #4
darenw
Member
 
Registered: Mar 2009
Location: Very humid Central Florida
Distribution: ArchLinux
Posts: 30

Original Poster
Rep: Reputation: 1
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)
 
Old 12-04-2009, 03:33 PM   #5
darenw
Member
 
Registered: Mar 2009
Location: Very humid Central Florida
Distribution: ArchLinux
Posts: 30

Original Poster
Rep: Reputation: 1
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...
 
Old 12-04-2009, 05:51 PM   #6
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by darenw View Post
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)
Could you post the complete output of running the above command?

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?
 
Old 12-04-2009, 08:38 PM   #7
darenw
Member
 
Registered: Mar 2009
Location: Very humid Central Florida
Distribution: ArchLinux
Posts: 30

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by JohnGraham View Post
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?
That's it! Program executable builds fine.

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.

Last edited by darenw; 12-04-2009 at 09:24 PM. Reason: clarify wording
 
Old 12-04-2009, 09:26 PM   #8
darenw
Member
 
Registered: Mar 2009
Location: Very humid Central Florida
Distribution: ArchLinux
Posts: 30

Original Poster
Rep: Reputation: 1
btw, libbfd comes from the binutils package, and is up to date.
 
Old 05-31-2010, 02:08 AM   #9
lokesh_c2004
LQ Newbie
 
Registered: Nov 2008
Posts: 5

Rep: Reputation: 0
Posting the command which compile lsstack successfully

gcc -W -Wall -g -o lsstack lsstack.c -lbfd -liberty
 
  


Reply

Tags
gcc, linking


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with g++ linker. cannot find library. nkoplm Programming 2 06-19-2009 06:05 AM
Debugger selectively not using symbols in library with symbols Millenniumman Programming 5 03-25-2007 10:44 AM
Debugger selectively not using symbols in library with symbols Millenniumman Programming 1 03-03-2007 08:46 PM
Command to Finding Source files for a library file jonty_11 Programming 1 03-23-2005 06:17 PM


All times are GMT -5. The time now is 01:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration