Gcc question

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read this

Gcc question

Postby crow_se on Sun Jun 24, 2007 6:13 am

Hello all
as a Linux sort of newbie (used Unix about 10 years ago and tried SUSE 5), now using Cassandra, I decided to try and get back into C and C++.
I wrote the usual Hello program in C, hello.c, then gcc hello.c. The program compiled and ran ok.
I then tried C++, hello.cpp.

#include <iostream>
int main(void)
{
cout << "Hello";
cout << "\n";
return 0;
}

In this case, gcc hello.cpp gave the following :
hello.cpp: In function ‘int main()’:
hello.cpp:6: error: ‘cout’ was not declared in this scope

iostream defines cout as external ostream.

can anyone tell me where I have gone wrong ? I am sure it is something really basic that I should have done.
TIA
stuart
crow_se
Level 1
Level 1
 
Posts: 11
Joined: Thu May 17, 2007 6:41 am

Linux Mint is funded by ads and donations.
 

Postby scorp123 on Sun Jun 24, 2007 10:56 am

User avatar
scorp123
Level 8
Level 8
 
Posts: 2287
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Gcc question

Postby crow_se on Sun Jun 24, 2007 1:56 pm

Thanks for the reply.
I followed the link and the usage was stated as
#include <iostream>

int main(void)
{
std::cout <<"Hello c++";
std::cout <<"\n";
return 0;
}

This std::cout rather fazed me since it is not something I have come across before, and there is no mention of this in Stroustrup. Have things changed ?

gcc hello.cpp then led to
/tmp/ccwWbOoT.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccwWbOoT.o: In function `__tcf_0':
hello.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccwWbOoT.o: In function `main':
hello.cpp:(.text+0x8e): undefined reference to `std::cout'
hello.cpp:(.text+0x93): 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*)'
hello.cpp:(.text+0xa2): undefined reference to `std::cout'
hello.cpp:(.text+0xa7): 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*)'
/tmp/ccwWbOoT.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

stuart
crow_se
Level 1
Level 1
 
Posts: 11
Joined: Thu May 17, 2007 6:41 am

Re: Gcc question

Postby HackingYodel on Thu Jun 28, 2007 1:38 am

crow_se wrote:Thanks for the reply.
I followed the link and the usage was stated as
#include <iostream>

int main(void)
{
std::cout <<"Hello c++";
std::cout <<"\n";
return 0;
}

This std::cout rather fazed me since it is not something I have come across before, and there is no mention of this in Stroustrup. Have things changed ?

gcc hello.cpp then led to <snip>


use g++ for your C++ source and gcc for C files. Please see http://users.actcom.co.il/~choo/lupg/tu ... source_cpp for more information.

Stroustrup does mention std in his C++ style and technique faq http://www.research.att.com/~bs/bs_faq2 ... le-program
It's that
Code: Select all
 using namespace std;
allows you to use
Code: Select all
cout << "Hello C++"
instead of
Code: Select all
 std::cout << "Hellow C++"


Does this help? I have a knack for complicating things.
User avatar
HackingYodel
Level 1
Level 1
 
Posts: 7
Joined: Tue Jun 26, 2007 2:04 am

Postby speaker219 on Thu Jun 28, 2007 1:48 pm

i just compiled it using g++ and it works, so just use that:
Image
speaker219
Level 2
Level 2
 
Posts: 56
Joined: Tue Jun 19, 2007 4:54 pm


Return to Other topics

Who is online

Users browsing this forum: No registered users and 3 guests