LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2006, 05:00 AM   #1
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Rep: Reputation: 16
"Hello World!" ... c++ and gcc question


I bought a book because I want to learn the c++ programming language. The first example program in the book is:

#include <iostream>

int main()
{
std::cout << "Hello World!\n";
return 0;
}

but when I save it as hello.cpp and try to compile I get:

[falcon56215@localhost Documents]$ gcc hello.cpp
/home/falcon56215/tmp/ccyTBJNM.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/home/falcon56215/tmp/ccyTBJNM.o: In function `__tcf_0':
hello.cpp.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/home/falcon56215/tmp/ccyTBJNM.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:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/home/falcon56215/tmp/ccyTBJNM.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[falcon56215@localhost Documents]$

Does anyone know why this happens? Is there another package I need to install to be able to compile c++?

Thanks,
Danny
 
Old 12-10-2006, 05:03 AM   #2
kamran_pro
LQ Newbie
 
Registered: Sep 2005
Posts: 28

Rep: Reputation: 15
Use g++ instead of gcc
 
Old 12-10-2006, 05:54 AM   #3
falcon56215
Member
 
Registered: Jan 2002
Location: Tennessee
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108

Original Poster
Rep: Reputation: 16
Thanks, I knew it had to be something simple. I'll just keep repeating "no studpid questions, no stupid questions....."

Danny
 
Old 12-10-2006, 07:00 AM   #4
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Moved: This thread is more suitable in the Programming Forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-10-2006, 07:09 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by falcon56215
Does anyone know why this happens?
It's because you're not linking the C++ library. Using g++ does this automatically for you, but with gcc you have to add a "-lstdc++".
 
Old 12-10-2006, 07:17 AM   #6
NDR008
Member
 
Registered: Nov 2006
Location: (Bristol or Coventry) (UK) or Malta
Distribution: openSUSE 11.0
Posts: 173

Rep: Reputation: 30
dumb question, why some people (me one of them) write:

cout << "Hello World!" << endl;

instead of

std::cout << "Hello World!\n";

What's the difference in terms of C++ standard?
 
Old 12-10-2006, 08:14 AM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
The endl might vary depending on the system's definition of a newline. Some systems use a \r\n (like MS DOS and Windows) and *nix uses just \n. endl also flushes the stream if it is buffered.

http://www.cplusplus.com/ref/iostrea...eam/_endl.html
 
Old 12-11-2006, 08:58 AM   #8
pasteNoctem
Member
 
Registered: Apr 2006
Location: Athens Greece
Distribution: Kubuntu
Posts: 35

Rep: Reputation: 15
Quote:
Originally Posted by NDR008
dumb question, why some people (me one of them) write:

cout << "Hello World!" << endl;

instead of

std::cout << "Hello World!\n";

What's the difference in terms of C++ standard?
why don't you just write cout<<"Hello World!\n";

i think it is simplier
 
Old 12-11-2006, 10:52 AM   #9
NDR008
Member
 
Registered: Nov 2006
Location: (Bristol or Coventry) (UK) or Malta
Distribution: openSUSE 11.0
Posts: 173

Rep: Reputation: 30
Because somewhere i was reading that cout << "Hello World!\n"; is not standard C++, something like that. When I get back home I will post the links to the sties I bookmarked regarding the argument, maybe someone can shed better light. As I like to think that *NIX oriented programmers are the real c++ programmers.
 
Old 12-11-2006, 11:09 AM   #10
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
From a "Learn C++"-type book I have, here's the helloworld.cpp example from it.
Code:
/* helloworld.cpp */

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void) {
  cout << "Hello World" << endl;
}
Note the "using namespace std". I have no idea what namespaces are, or their applications and functions, but I think that may have something to do with the "std::cout".

Last edited by indienick; 12-11-2006 at 11:38 AM.
 
Old 12-11-2006, 11:19 AM   #11
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Namespaces are used to avoid clashing of symbols in the global namespace (or the default namespace). I'm sure they have something to do with compiler and linker optimization as well, but their basic use is to make code more readable by logically separating related classes, functions and variables into their own "namespace".
 
Old 12-11-2006, 11:37 AM   #12
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Sweet. Thanks for the enlightenment harishankar.
 
Old 12-11-2006, 11:39 AM   #13
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
You might also want to look at this as well:
http://www.cplusplus.com/doc/tutorial/namespaces.html
 
Old 12-12-2006, 05:59 AM   #14
pasteNoctem
Member
 
Registered: Apr 2006
Location: Athens Greece
Distribution: Kubuntu
Posts: 35

Rep: Reputation: 15
Quote:
Originally Posted by indienick
From a "Learn C++"-type book I have, here's the helloworld.cpp example from it.
Code:
/* helloworld.cpp */

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void) {
  cout << "Hello World" << endl;
}
You don't have to include all those things, if you don't mind some warnings that is.

I think that you should look for the warnings when making huge programs or commercial ones, and evaluate them..
 
Old 12-12-2006, 09:58 AM   #15
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Thanks, but like I said, it's just an example I copied directly from a book. I don't program in C++, in the least, but thanks for the tip.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 04:08 PM
LXer: Jon "maddog" Hall on FOSS in the developing world LXer Syndicated Linux News 0 06-08-2006 08:21 AM
Porting to Fedora gcc 4.0 -parsing backward slash("\") comma(",") code_blew Programming 1 04-26-2006 04:07 PM
LXer: "Bring Open Source To the World Of BPM" LXer Syndicated Linux News 0 12-12-2005 06:01 AM
gcc "Hello world" wont... aes canis Programming 13 05-13-2005 02:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration