"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::operator<< <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 |
Use g++ instead of gcc
|
Thanks, I knew it had to be something simple. I'll just keep repeating "no studpid questions, no stupid questions....."
Danny |
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.
|
Quote:
|
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? |
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 |
Quote:
i think it is simplier :p |
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.
|
From a "Learn C++"-type book I have, here's the helloworld.cpp example from it.
Code:
/* helloworld.cpp */ |
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".
|
Sweet. :) Thanks for the enlightenment harishankar.
|
You might also want to look at this as well:
http://www.cplusplus.com/doc/tutorial/namespaces.html |
Quote:
I think that you should look for the warnings when making huge programs or commercial ones, and evaluate them.. |
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. :D
|
All times are GMT -5. The time now is 03:13 AM. |