PDA

View Full Version : This code won't compile in Red Hat Linux 9



array
04-21-2004, 04:35 PM
Hi all,

I've been using Dev C++ compiler in windows for C & C++.
I started out reading Schildt's book, "Teach Yourself C" which I finished a couple months ago. Then a friend who is going to school for computing said that he was taking a C++ course and suggested that I give C++ a try. I already had Jesse Liberty's book, "Teach Yourself C++ in 21 days which was put out in 1999 and I worked with it a little bit back then and the examples from book compiled fine in Linux and in Windows. I heard that the standards had changed but Schildt's Book "Teach Yourself C++" 3rd Edition lived up to those standards.

So heres my problem.

This next bit of code works fine in Windows but I get errors in Red hat Linux 9.0

I'll just write out a little example code:

#include <iostream>
using namespace std;

int main()
{

cout << "Just a test\n";

return 0;
}

This won't code won't compile for me in Red hat 9.

Can someone please show me what I need to do to get this code compiled.

Thanks so much

Prelude
04-21-2004, 04:46 PM
>but I get errors in Red hat Linux 9.0
And those errors are...

>Can someone please show me what I need to do to get this code compiled.
Gladly. Show us how you're doing it and we'll tell you what you're doing wrong.

Salem
04-21-2004, 05:01 PM
You did remember to use g++ for compiling C++, and not gcc which is used to compile C

array
04-22-2004, 04:03 PM
Hi Prelude & Salem. I have to tell you both that I didn't even know about the "g++ thing." It was about 5 years ago that I last fooled around with C++ so I guess they changed things around somewhere in between then and now. Anyway, the program compiled. Thank you both for replying.

awoodland
04-24-2004, 11:00 AM
gcc will happily compile c++ code for you, you do however need to tell it to link against the standard c++ library when it calls the linker. Ie:

gcc -lstdc++ myfile.cc -o myfile

Alan

rotis23
04-27-2004, 05:39 AM
I think it can also look at the extension of the source code file.

awoodland
04-27-2004, 05:56 AM
woodalan@jehovah:~ > gcc test.cc -o test
/tmp/ccxrJYpP.o(.text+0x1b): In function `main':
: undefined reference to `std::cout'
/tmp/ccxrJYpP.o(.text+0x20): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

.....





woodalan@jehovah:~ > gcc test.cc -o test -lstdc++
woodalan@jehovah:~ > ./test
TEST


Looks like it can't find the symbols unless you use -lstdc++ to me. Could just be my version of gcc

Alan

XSquared
04-27-2004, 06:30 AM
>Looks like it can't find the symbols unless you use -lstdc++ to me.
That's because, as Prelude and Salem said, gcc is a C compiler by default. If you don't want to use -lstdc++ each time, use g++.