|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
"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. |
|
#2
|
|||
|
|||
|
Quote:
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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 Code:
g++ file1.cpp file2.cpp -lm Code:
g++ file1.cpp file2.cpp -lm -lSDL Code:
g++ -c file1.cpp -lm -lSDL -o file1.o g++ -c file2.cpp -lm -lSDL -o file2.o g++ file1.o file2.o |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
||||
|
||||
|
Quote:
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
Quote:
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 |
|
#9
|
|||
|
|||
|
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... |
|
#10
|
||||
|
||||
|
Quote:
Quote:
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. |
|
#11
|
||||
|
||||
|
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/ |
|
#12
|
||||
|
||||
|
Quote:
Clifford |
|
#13
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > "undefined reference to" in C++ (using g++) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|