|
-
December 4th, 2003, 04:23 AM
#1
Linking problem - "undefined reference"
Hi,
I try to Link my compiled object file with gcc, but
it gives me 50 lines of "undefined reference" errors, despite
the compile phase goes fine?!
Code:
...
Utils.o(.text+0xf):Utils.cpp: undefined reference to `operator new(unsigned long)'
Utils.o(.text+0x2e):Utils.cpp: undefined reference to `operator delete(void*)'
Utils.o(.text+0x5e):Utils.cpp: undefined reference to `std::ios_base::Init::Init()'
Utils.o(.text+0xbc):Utils.cpp: undefined reference to `std::ios_base::Init::~Init()'
Utils.o(.gnu.linkonce.t._ZN5Utils7printDescriptionEv+0xf):Utils.cpp: undefined reference to `std::cout'
...
well, I have installed all the required libraries for gcc, I think,
These are what I have included in the source file:
Code:
using namespace std;
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "FileUtils.cpp"
-
December 4th, 2003, 08:19 AM
#2
You have installed them, but do you reference them? You seem to be lacking a reference to the standard C++ library.
All the buzzt
CornedBee
-
December 4th, 2003, 08:51 AM
#3
>You seem to be lacking a reference to the standard C++ library.
well, I´m migrating from Java to C++ and I`m a bit puzzled.
Could you advice me a bit more.
Do I have to tweak gcc options or do I have something
wrong with my code?
-
December 4th, 2003, 08:56 AM
#4
It's the compiler options. Think of it as the class path.
gcc source.cpp -lstdc++
or something like that. Order matters!
All the buzzt
CornedBee
-
December 4th, 2003, 09:29 AM
#5
ummmm... I`m lost. I`v tried to search to manual
for an answer...but no,
as far I understand, I have to inform the linker about
some libraries. But I don`t know what are the names of
those and where they reside?
If I try to do the whole thin as follows:
gcc -Wall -o myprog.exe mymain.c
-> "undefined reference to..."
Do I have to use the "-Xlinker" switch? If so, how to use it.
..ah, it`s allways pain in the beginning
-
December 4th, 2003, 10:48 AM
#6
I mean, with -l or -L flag. But what to include?
-
December 4th, 2003, 11:07 AM
#7
-lc for the standard C library.
I think -lstdc++ for the standard C++ library.
Then there are other libraries: to get libpng.a you'd write
-lpng
I just noticed you #include a .cpp. Don't ever do that!
All the buzzt
CornedBee
-
December 4th, 2003, 11:29 AM
#8
I just tried the following
Code:
C:\projects\test>gcc -lstdc++ -o Test.exe Test.o
and with this
and it produced:
Code:
c:/djgpp/bin/ld.exe: cannot find -lstdc++
collect2: ld returned 1 exit status
does this mean I don`t have that library??
I just noticed you #include a .cpp. Don't ever do that!
hmm, I have seen two "ways" of coding with C++.
1. "Like" in java, you wite the .cpp file. No header file.
2. make a header file, and then the .cpp file, like:
MyClass::MyMethod()
{ ... }
whitout enclosing the functions in a class scope ( like in Java)
-
December 4th, 2003, 11:38 AM
#9
Been a long time since I used gcc this way. Never MingW. I really don't know.
All the buzzt
CornedBee
-
December 4th, 2003, 12:04 PM
#10
Since you are using gcc, I suggest getting one of the IDE's that complement gcc. Dev C++ (aka Bloodshed C++) is an IDE for gcc, and it's free. You don't worry about all of these problems of linking, since they have built into the IDE the proper linker flags and paths.
Originally posted by halmark6Z
hmm, I have seen two "ways" of coding with C++.
1. "Like" in java, you wite the .cpp file. No header file.
2. make a header file, and then the .cpp file, like:
MyClass::MyMethod()
{ ... }
whitout enclosing the functions in a class scope ( like in Java)
Please do not use Java as a model to programming C++. In C++, header files usually contain function prototypes and struct and class definitions, as well as #defined constants (aka macros). C++ header files are simply files that contain definitions that you would want to include in more than one CPP file. They should not be compared to Java's #import, which (at least to me) is more than just an "insert file here" command.
Just as an addendum, there was another poster a few weeks ago who was insistent on writing C++ like Java, since they came from a Java background. He/she was creating some sort of Object class similar to Java, so that they can create a Vector class. All of this was unnecessary, since C++ already has a vector class. I'm saying this because there are Java programmers who want to turn C++ into Java so as to be more "comfortable", but that isn't the way to go about learning proper C++.
Regards,
Paul McKenzie
-
December 4th, 2003, 12:16 PM
#11
instead of typing "gcc" try "g++"
g++ source.cpp
-
December 4th, 2003, 04:00 PM
#12
Finally, I got it working. Thank for helping, really!
I used gxx instead of g++
Well, I tried a bit further.
I made a simple class to do set/get stuff.
Then I created a file with a main function and I tried
to use my DerivedSimpleClass there ( compiled to DerivedSimpleClass.o ) but
the linker prompted:
Code:
C:\projects\test>gxx -o -lstdcxx Test.o DerivedSimpleClass.o SimpleClass.o
Test.o(.gnu.linkonce.t._ZN8SimpleClassC2Ev+0x8):Test.cpp: undefined reference to `vtable for SimpleClass
Test.o(.gnu.linkonce.t._ZN14DerivedSimpleClass15getContentEv+0xf):Test.cpp: undefined reference to `SimpleClass::getContent()'
Test.o(.gnu.linkonce.t._ZN8SimpleClassD2Ev+0x7):Test.cpp: undefined reference to `vtable for SimpleClass
collect2: ld returned 1 exit status
So I guess I`m using the linker wrong again?
-
December 4th, 2003, 04:12 PM
#13
Try switching changing the order of the object files. ld can be picky.
All the buzzt
CornedBee
-
December 4th, 2003, 05:00 PM
#14
I tried, it complains about the same thing:
Code:
gxx -o Test.exe Test.o DerivedSimpleClass.o SimpleClass.o
gxx -o Test.exe SimpleClass.o DerivedSimpleClass.o Test.o
gxx -o Test.exe DerivedSimpleClass.o Test.o SimpleClass.o
they all produce:
Code:
Test.o(.gnu.linkonce.t._ZN14DerivedSimpleClass15getContentEv+0xf):Test.cpp: undefined reference to `SimpleClass::getContent()'
collect2: ld returned 1 exit status
and the code
Code:
//SimpleClass.cpp
class SimpleClass
{
public:
virtual string getContent() = 0;
};
//DerivedSimpleClass.cpp
class DerivedSimpleClass : public SimpleClass
{
public:
string getContent()
{
return "let me out";
}
};
// Test.cpp
int main(int argc, char* argv[])
{
// complains about this ?!
DerivedSimpleClass *notworking = new DerivedSimpleClass();
cout<<notworking->getContent();
}
Well, how does everybody in general deal with multiple .o files
-
December 4th, 2003, 06:22 PM
#15
fixed...I see light in the horizon....
inheritance bug.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|