strange compile error

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
zacharyleung
New poster
Posts: 14
Joined: Tue Feb 03, 2004 3:43 am

strange compile error

Post by zacharyleung »

This is a really strang error that I get! Any idea what's wrong?

[cpp]
#include <iostream>

int main() {
int i;
cout << "hello" << endl;
cin >> i;
return 0;
}
[/cpp]

Code: Select all

leungnga@sf3:~/CS3233[518]$ gcc Test.cc
Undefined                       first referenced
 symbol                             in file
ostream::operator<<(char const *)   /var/tmp/cc65MrC2.o
cout                                /var/tmp/cc65MrC2.o
cin                                 /var/tmp/cc65MrC2.o
endl(ostream &)                     /var/tmp/cc65MrC2.o
istream::operator>>(int &)          /var/tmp/cc65MrC2.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/cc65MrC2.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.
(John 3:16)
Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Post by Piotrek Mazur »

You have to put this in your code after including libraries:
[cpp]using namespace std;
[/cpp]
abishek
Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

Post by abishek »

try doing the following
below the line containg

'#include <iostream>"
add
using namespace std;

you may also try compiling with the command g++ Test.cc
bye
abi
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

gcc and g++ means the same for C++ files. gcc is smart enough to switch to g++ in such case, so there should be no difference.
zacharyleung
New poster
Posts: 14
Joined: Tue Feb 03, 2004 3:43 am

Post by zacharyleung »

Ok, I changed the program to:
[cpp]
#include <iostream>

using namespace std;

int main() {
int i;
cout << "hello" << endl;
cin >> i;
return 0;
}
[/cpp]

But I get exactly the same problem!

Just a note: I'm not doing this on my own computer. I'm SSH-ing to a remote computer (a university comp) and compiling it there. Could the problem be that there's something wrong with one of their files that is used by the compiler?
For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.
(John 3:16)
Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Post by Piotrek Mazur »

Krzysztof Duleba wrote:gcc and g++ means the same for C++ files. gcc is smart enough to switch to g++ in such case, so there should be no difference.
But there is a difference. Try to make this:

Code: Select all

g++ Test.cc
and you won't have any errors.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Piotrek - which version of g++ and gcc do you have? This behaviour is possible only if you have g++ in version < 3 and a newer gcc (quite a strange mix as they are usually distributed together). Otherwise there should be no difference.

zacharyleung - what version of gcc do you have? Do you happen to use any command line switch? Your problem is not with the compiler though, but with linker (ld). Ask your system administrator about it - it seems that gcc is installed inappriopriately. Your TMP environmental variable possibly has a strange value - check it out too.
Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Post by Piotrek Mazur »

I have gcc & g++ version 3.2.2 (Red Hat 9.0) - and when I compile this on gcc I have an error, on g++ - I have not.
I know it's strange, but it's true :-?
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

This is not just strange - it's a bug (even two). First is that g++ should accept global stream names (cin/cout/cerr) only if std namespace is used. Second is that gcc behaves the same way in case of C++ files. In fact, they both do nothing and call cc1plus. To see that, test the following code
[cpp]template<class T>
struct st{
st<st<T> > a;
};

int main(){
st<int> t;
}[/cpp]
Now compile it with gcc and then with g++. If the code compiles too fast due so small default value of ftemplate-depth, use the following switch:
-ftemplate-depth-500
The command in case of g++ should look like
g++ -ftemplate-depth-500 t.cpp & ps
Then repeat it using gcc instead of g++.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Or you can just compile anything (delay it with sleep if necessary) and run top on second console :-)
zacharyleung
New poster
Posts: 14
Joined: Tue Feb 03, 2004 3:43 am

Post by zacharyleung »

zacharyleung - what version of gcc do you have? Do you happen to use any command line switch? Your problem is not with the compiler though, but with linker (ld). Ask your system administrator about it - it seems that gcc is installed inappriopriately. Your TMP environmental variable possibly has a strange value - check it out too.
I have gcc version 2.95.2. You're right, it compiles ok with g++ but not with gcc. That's quite strange! What's a command line switch? Is this a problem because the gcc version is too old, and the problem is fixed in the newest versions, or is it a linker problem or the TMP environemental variable? My TMP variable has no contents.

Code: Select all

echo $TMP

Krzysztof Duleba, thanks for your help. The thing I'm wondering is this: you suggesting compiling some code. What is the purpose of doing that suggested code? What does it illustrate?
For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.
(John 3:16)
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

you suggesting compiling some code. What is the purpose of doing that suggested code?
The code is compiling so long that it's easily possible to see that processes are running. And in both cases (no matter if you start with g++ or gcc) it is cc1plus, and if compilation is successful it is followed by ld. So there should be no difference. If there is, there's something wrong with gcc 2.95.
You're right, it compiles ok with g++ but not with gcc
No - Piotrek is right. If g++ command works right, then go for it - it means that Piotrek had a good idea. I use make personally (for instance `make foo' is equivalent for `g++ foo.cpp -o foo' if foo.cpp file exists).
Is this a problem because the gcc version is too old, and the problem is fixed in the newest versions
Yes, in newer versions the code works (or doesn't work depending if you add "using namespace std;" or not) as expected.
My TMP variable has no contents
That's funny. Don't ask me why I think so, but I find it very funny :-)
Post Reply

Return to “C++”