ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Xubuntu Natty on Lenovo R61i Thinkpad
Posts: 108
Rep:
"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++?
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.
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.
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".
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.