LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2005, 08:13 AM   #1
dangerousdave
Member
 
Registered: Feb 2005
Distribution: Ubuntu 8.04
Posts: 100

Rep: Reputation: 15
What does "undefined reference to ..." mean when using gcc


I'm triying to compile a small program I wrote that uses a library I recently installed and I get several "undefined reference to ..." errors for the functions I've used from the library. Can anyone tell me what this error means so I can figure out whats causing it?

Cheers,
 
Old 03-10-2005, 08:23 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
That means your calling a function that has no definition. Either you haven't included the include files that define the functions or you haven't linked the library in with the -l<libraryname> switch on compile.

An example... if you want to compile something with libpthread you would all the -lpthread switch (notice that it is -lpthread and NOT -libpthread). "gcc -Wall -o TestProgram -lpthread TestProgram.c"
 
Old 03-10-2005, 08:45 AM   #3
dangerousdave
Member
 
Registered: Feb 2005
Distribution: Ubuntu 8.04
Posts: 100

Original Poster
Rep: Reputation: 15
OK, at the top of my code I have:
Code:
#include <stdio.h>
#include "playerc.h"
I have playerc.h in my /usr/local/include directory. If I change the include line to something invalid I get a different error, so it is definately finding the playerc.h file.

I compile with:
gcc -Wall -o control -lplayerc control.c

I have libplayerc.a in my /usr/local/lib directory, yet I still get undefined reference errors.

Now, if I compile with just:
gcc -Wall -o control control.c

...I get exactly the same error. What does this mean, something not linking right? If I compile with an invalid library I get a different error again, so it is finding the library, just not looking at it for some reason...

Any ideas?

Cheers,
 
Old 03-10-2005, 08:47 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-10-2005, 09:09 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I'm not sure how it works with static libraries, but you might try running ldconfig as root and then trying to compile with the -lplayerc option again. If that doesn't work then gcc -Wall -o control control.c /usr/local/lib/libplayerc.a should definitely work.
 
Old 03-11-2005, 07:33 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it matters what order you do the linking.

It's the loader.
It only sees reference in a left<-right order.

It opens the lib, says: "are there any references I can fulfill?" then closes it. It does NOT go back
for another pass. So as your -o file comes before the lib,
it fails.

try changing the order:

Code:
gcc -Wall     -o control  control.c  -lplayerc
observe:

Code:
billym.primadtpdev>gcc  -o all -lmylib  all.c
Undefined                       first referenced
 symbol                             in file
hello                               /var/tmp/ccgQa4rR1.o
bye                                 /var/tmp/ccgQa4rR1.o
ld: fatal: Symbol referencing errors. No output written to all
Code:
billym.primadtpdev>gcc  -o all  all.c -lmylib
billym.primadtpdev>all
Hello
bye
 
Old 03-11-2005, 08:22 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
or try:

Code:
make  control LDLIBS=-lplayerc
 
  


Reply



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
"undefined reference"linker error for static field w/ C++ astorm Programming 5 08-27-2008 03:00 AM
Curl - undefined reference "DES_set_odd_parity" - SOLVED sundialsvcs Linux - Software 0 09-15-2005 09:58 AM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
Kernel make bzImage error "undefined reference" w/ Prism2.5 USB driver under 2.6 joachimvb Mandriva 1 09-24-2004 07:43 PM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 AM.

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
Open Source Consulting | Domain Registration