Page 1 of 1
strange compile error
Posted: Sat Jul 31, 2004 8:57 am
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
Posted: Sat Jul 31, 2004 9:23 am
by Piotrek Mazur
You have to put this in your code after including libraries:
[cpp]using namespace std;
[/cpp]
Posted: Sat Jul 31, 2004 9:24 am
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
Posted: Sat Jul 31, 2004 5:07 pm
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.
Posted: Sun Aug 01, 2004 9:33 am
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?
Posted: Sun Aug 01, 2004 10:34 am
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:
and you won't have any errors.
Posted: Sun Aug 01, 2004 5:50 pm
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.
Posted: Sun Aug 01, 2004 9:50 pm
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

Posted: Sun Aug 01, 2004 10:45 pm
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++.
Posted: Sun Aug 01, 2004 10:52 pm
by Krzysztof Duleba
Or you can just compile anything (delay it with sleep if necessary) and run top on second console

Posted: Mon Aug 02, 2004 3:40 am
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.
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?
Posted: Mon Aug 02, 2004 4:22 am
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
