LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "Hello World!" ... c++ and gcc question (https://www.linuxquestions.org/questions/programming-9/hello-world-c-and-gcc-question-509036/)

falcon56215 12-10-2006 05:00 AM

"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

kamran_pro 12-10-2006 05:03 AM

Use g++ instead of gcc

falcon56215 12-10-2006 05:54 AM

Thanks, I knew it had to be something simple. I'll just keep repeating "no studpid questions, no stupid questions....."

Danny

reddazz 12-10-2006 07:00 AM

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.

Nylex 12-10-2006 07:09 AM

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++".

NDR008 12-10-2006 07:17 AM

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?

vharishankar 12-10-2006 08:14 AM

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

pasteNoctem 12-11-2006 08:58 AM

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 :p

NDR008 12-11-2006 10:52 AM

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.

indienick 12-11-2006 11:09 AM

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". :)

vharishankar 12-11-2006 11:19 AM

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".

indienick 12-11-2006 11:37 AM

Sweet. :) Thanks for the enlightenment harishankar.

vharishankar 12-11-2006 11:39 AM

You might also want to look at this as well:
http://www.cplusplus.com/doc/tutorial/namespaces.html

pasteNoctem 12-12-2006 05:59 AM

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..

indienick 12-12-2006 09:58 AM

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.