C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2009, 01:44 PM
wbest wbest is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 55 wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 25 m 7 sec
Reputation Power: 19
"undefined reference to" in C++ (using g++)

I'm trying to compile a piece of code, but I keep getting the error:

undefined reference to X

where X is either a variable, or a function.

I get this despite the fact that those items appear to be in the header files, in some cases, right after a #define statement.

Why would I be getting this? It seems like 90% of the C++ files that are part of this project have this problem, I think for different things, but after the third or so I file I tested, I stopped bothering to read the whole line.

Reply With Quote
  #2  
Old June 24th, 2009, 02:08 PM
jcampos8782 jcampos8782 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 53 jcampos8782 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 35 sec
Warnings Level: 5
Reputation Power: 0
Quote:
Originally Posted by wbest
I'm trying to compile a piece of code, but I keep getting the error:

undefined reference to X

where X is either a variable, or a function.

I get this despite the fact that those items appear to be in the header files, in some cases, right after a #define statement.

Why would I be getting this? It seems like 90% of the C++ files that are part of this project have this problem, I think for different things, but after the third or so I file I tested, I stopped bothering to read the whole line.


It looks to me like you have an undefined reference to X If you post the code, we would probably have a lot more to work with... the exact error would help also.

Reply With Quote
  #3  
Old June 24th, 2009, 02:42 PM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,139 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 1 Day 23 h 22 m 21 sec
Reputation Power: 861
You should post some example error messages.

Undefined reference is typically a linker error, not a compiler error.

Typical causes of these are:
- missing functionality in the project, did you compile ALL the source files
- typo's in symbol names, myFunc != MyFunc
- missing libraries, where you're trying to make use of 3rd party code.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Reply With Quote
  #4  
Old June 24th, 2009, 02:50 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,696 Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level)Lux Perpetua User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 19 h 7 m 7 sec
Reputation Power: 928
Including a header file only puts the declarations of external variables and functions into your code so the compiler can check that you're using them correctly. It makes no guarantee that those items will actually be found. Consider
Code:
g++ file1.cpp file2.cpp
If those files refer to variables or functions that aren't defined in those two files, then the compiler will treat them as external references and leave the linker to figure out what they point to. Sometimes, those references are to things in the standard library (libstdc++) that the C++ linker will resolve automatically without having to be told. Most of the time, however, you need to pass an argument to g++ to tell the linker which library or libraries contain the items your code refers to. The most common example that trips up beginners is the math library: if you use functions in math.h, you will probably need to compile your code as:
Code:
g++ file1.cpp file2.cpp -lm
For any third-party library that is installed on your system, there are corresponding linker flags that you will need to use. If you use the math library and the SDL library, you would use:
Code:
g++ file1.cpp file2.cpp -lm -lSDL
Another common cause of undefined symbols is when you have multiple source files of your own that you want to compile separately, but each file refers to variables or functions defined in the other files. In this case, you need to compile each file into an object file, not a complete executable, and then link the various object files together:
Code:
g++ -c file1.cpp -lm -lSDL -o file1.o
g++ -c file2.cpp -lm -lSDL -o file2.o
g++ file1.o file2.o
I think that covers the most common causes for undefined symbols. If that doesn't help, post the complete compilation command and error message.

Reply With Quote
  #5  
Old June 24th, 2009, 03:58 PM
wbest wbest is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 55 wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 25 m 7 sec
Reputation Power: 19
Okay, I was working on compiling some later code, and I think I found where they are defined (I HOPE its there). Anyway, some of the other code has some problems I have to deal with first (windows.h, for example) before I can try and compile all the code together in one call. Once I feel it is close to working I'll try compiling it all and sharing the results.

Reply With Quote
  #6  
Old June 25th, 2009, 07:37 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,712 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 23 h 23 m 3 sec
Reputation Power: 1165
Quote:
Originally Posted by wbest
Anyway, some of the other code has some problems I have to deal with first (windows.h, for example)
If you think there is a problem with the code in <windows.h> you are probably wrong!

Reply With Quote
  #7  
Old June 30th, 2009, 12:50 PM
wbest wbest is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 55 wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 25 m 7 sec
Reputation Power: 19
Sorry, for responding late...
If I'm not mixing this up with another solution to a problem I had, what worked for me was to compile all the files together (I took out that reference to windows.h on suggestion in another thread).
So yeah, all those undefined references where defined in other files in the same project.

Reply With Quote
  #8  
Old July 1st, 2009, 05:31 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,712 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 23 h 23 m 3 sec
Reputation Power: 1165
Quote:
Originally Posted by wbest
what worked for me was to compile all the files together
That is an ambiguous statement, and may either mean you did the right thing or a rather silly thing! You either have the terminology or the practice wrong.

All the project files should be compiled separately then linked together not compiled together.

What I am hoping you did not do was #include all the files into one large module, which may conceivably be what you meant.

Clifford

Reply With Quote
  #9  
Old July 1st, 2009, 08:46 AM
wbest wbest is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 55 wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level)wbest User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 25 m 7 sec
Reputation Power: 19
What I meant was, I compiled them all at the same time...so I guess I linked them together.
What I wrote was basically:

g++ foo1.cpp foo2.cpp foo3.cpp ... fooX.cpp

Maybe I have my terminology wrong, but I'm sending them all to the compile in the same command, so I'm compiling them together. The compiler, I assume, has an algorithm to handle such an action, wherein any shared functions or variables are taken care of, and is not really compiling them all together, nor simultaneously; but at the level of abstraction I am looking at it, as a user, it is a black box that is (as I see it) compiling them all together.

Sorry, just had to defend my word choice

But yeah, maybe linking them is a better choce of words...

Reply With Quote
  #10  
Old July 1st, 2009, 11:51 AM
Oler1s Oler1s is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2006
Posts: 1,984 Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level)Oler1s User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 5 Days 9 h 15 m 6 sec
Reputation Power: 1083
Quote:
Maybe I have my terminology wrong, but I'm sending them all to the compile in the same command, so I'm compiling them together.
You have your terminology wrong.

Quote:
The compiler, I assume, has an algorithm to handle such an action, wherein any shared functions or variables are taken care of, and is not really compiling them all together, nor simultaneously; but at the level of abstraction I am looking at it, as a user, it is a black box that is (as I see it) compiling them all together.
No it doesn't. While it makes sense to treat a compiler as a black box in some senses, the compilation mechanism shouldn't be ignored.

It matters. Terminology matters. Details matter. Shared functions? Functions that are defined in one source file and used in another? It's not automatically taken care of. You need to declare it. You need to make sure you have only one definition, and that every other file that uses it has the correct signature declared.

"Compiling together" makes no sense. Each file is compiled independently, and then linked together. Your usage of the compiler and the command line is irrelevant.
__________________
When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.

Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
Want better answers? Tell us what you Googled for and what steps you took to answer your own question.

Reply With Quote
  #11  
Old July 1st, 2009, 12:00 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,388 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 53 m 45 sec
Reputation Power: 563
As was noted by someone earlier, g++ and cc are just drivers, they aren't compilers. They can be used to cause the files you listed compiled one at a time and then linked together. Both drivers run a compiler followed by running a linker or an archiver (in the case of building a library). Read your man pages on g++. It's been a while, but I think the command line would be man g++.

__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #12  
Old July 1st, 2009, 03:40 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,712 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 23 h 23 m 3 sec
Reputation Power: 1165
Quote:
Originally Posted by wbest
What I wrote was basically:

g++ foo1.cpp foo2.cpp foo3.cpp ... fooX.cpp

Fair enough. Note that g++ (you seem to have changed compiler driver again!) is not only a compiler driver, it can also invoke the linker (and does so by default; use -c for compilation only) which is what is happening here. You can invoke the linker (ld) directly to link previously compiled files, or you can use the compiler driver to link previously compiled object files (no compilation). The advantage of using the compiler driver is that it knows what standard libraries to link by default.

Clifford

Reply With Quote
  #13  
Old July 1st, 2009, 04:07 PM
jwdonahue's Avatar
jwdonahue jwdonahue is online now
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,388 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 53 m 45 sec
Reputation Power: 563
You might also want to look into using a make file for your builds. This can allow you define dependencies between source, object and binary files so that whatever has modified will be updated accordingly. Particularly useful when you have a lot of files to compile and link.

man make

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > "undefined reference to" in C++ (using g++)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
Stay green...Green IT