LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 07-23-2005, 03:37 AM   #1
flamesrock
Member
 
Registered: Aug 2003
Distribution: Gentoo 2006.1
Posts: 405

Rep: Reputation: 30
C++ Hello World >> help!


Hi,

So, I'm actually a python programmer, but I've decided to learn c++ as I want to get into 3d programming with irrlicht.

Here is the program I tried to compile:
Code:
// my first program in C++

#include <iostream.h>

int main ()
{
  cout << "Hello World!";
  return 0;
}
Here is the output:
gcc test.cpp
Code:
In file included from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5-20050130/include/g++-v3/backward/iostream.h:31,
                 from test.cpp:3:
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.5-20050130/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
test.cpp:9:2: warning: no newline at end of file
/tmp/ccDwuf0Y.o(.text+0x1b): In function `main':
: undefined reference to `std::cout'
/tmp/ccDwuf0Y.o(.text+0x20): In function `main':
: 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/ccDwuf0Y.o(.text+0x49): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccDwuf0Y.o(.text+0x7a): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccDwuf0Y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status


I have NEVER seen a hello world program not function correctly, and I've done it in c before so I don't know how the compiler had messed it up..


-thanks in advance
 
Old 07-23-2005, 03:43 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
First error is about the header files. Use

Code:
#include <iostream>
using namespace std;
instead of
Code:
#include <iostream.h>
Also, you should (I think) be compiling with "g++ test.cpp" and not "gcc test.cpp". I think that's why you're getting undefined reference to cout.

Last edited by Nylex; 07-23-2005 at 03:45 AM.
 
Old 07-23-2005, 04:17 AM   #3
flamesrock
Member
 
Registered: Aug 2003
Distribution: Gentoo 2006.1
Posts: 405

Original Poster
Rep: Reputation: 30
Thanks!

unfortunately, though a.out appears in the directory, it produces no output when I run it.

Also, g++ gives me the following error:
test.cpp:10:2: warning: no newline at end of file

Code:
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

What could that mean? The code appears correct.

-thanks
 
Old 07-23-2005, 04:19 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
That there's not a new line at the end of the file. Open your editor and after that closing brace, just hit enter and then save the file and compile again. I don't know why you need to put a new line there, however.
 
Old 07-23-2005, 04:21 AM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Code:
test.cpp:10:2: warning: no newline at end of file
That's not an error just a warning.

The gcc compiler expects a newline at the end of the source file. Simple enough. Shouldn't be a problem at all...

As for the program itself, it should produce the output. Are you running it from a console or are you trying to double click it from the GUI?

Last edited by vharishankar; 07-23-2005 at 04:23 AM.
 
Old 07-23-2005, 04:39 AM   #6
flamesrock
Member
 
Registered: Aug 2003
Distribution: Gentoo 2006.1
Posts: 405

Original Poster
Rep: Reputation: 30
going:

./a.out

On second observation, it works in bash but not zsh. Any experience with this problem?

(adding a new line did get rid of the warning)

-thanks
 
Old 07-23-2005, 09:30 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
actually the real reason its complaining is because there is no new line charecter at the end of your statement


should be

Code:
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!\n";
  return 0;
}
this is however the only program i know how to write in c++
i much prefer c because its more pretier and orginized.

here is the same program in c
Code:
#include <stdio.h> 

int main()

{
   printf("Hello world!\n"); 
   return 0; 
}

Last edited by exvor; 07-23-2005 at 09:32 AM.
 
Old 07-23-2005, 10:10 AM   #8
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
This forum is perfect to be sticked. No offences meant, but this two questions are asked every 2 weeks (references missing due to bad header file name and namespace + program that "does not run"). How can the moderators be contacted to do so?
 
Old 07-23-2005, 12:10 PM   #9
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally posted by enemorales
This forum is perfect to be sticked. No offences meant, but this two questions are asked every 2 weeks (references missing due to bad header file name and namespace + program that "does not run"). How can the moderators be contacted to do so?
There is a "report this post to a moderator" link, on the bottom right corner of every post. Following the link will provide you with a text box where you can explain why you are reporting this post. Actually you may report your own post to "force" the moderators notice your comments, but I really doubt this is going to be a sticky thread. There are 2-3 stuff asked all the time for every programming language. Maybe if someone sums those things up and creates a thread tittled "Read this before you post on this forum" they may do it a sticky thread, but if I were a moderator I wouldn't do this one sticky.

P.S.Of course this is just my opinion and I may be wrong

Last edited by perfect_circle; 07-23-2005 at 12:11 PM.
 
Old 07-23-2005, 12:21 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by exvor
actually the real reason its complaining is because there is no new line charecter at the end of your statement


should be

Code:
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!\n";
  return 0;
}
Nope, the reason was as others have already stated. There was no newline at the end of the file. Thus the reason the warning was:

test.cpp:10:2: warning: no newline at end of file

That happens a lot when you use Windows text editors because they generally won't add a newline at the end of the last line of a file when there is something else there.

I was trying to come up with a scenario with include files that this could cause problems, but wasn't able to since the #include statements are generally a line of their own so there is a newline character after the file is included already... I'm sure there is probably some rare scenario it could potentially cause problems, though.
 
Old 07-23-2005, 12:35 PM   #11
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
I believe there shouldn't be a space between main and the ().
So...

#include <iostream>
using namespace std;

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

... then in cli do "g++ program.cpp -o program" without the qoutes and you should have a fuctional program.
 
Old 07-23-2005, 05:36 PM   #12
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
I dont think this should waste space being stickied, it just shows the simplest usage of g++. Just answer the questions or point users to old threads, its not difficult to answer in 2 seconds flat, and I'd rather take that time to answer than see 5 "how to compile hello world programs in xxx language" at the top of the programming forum.

Code:
// hello.cpp - A hello world program
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World" << endl;
    return 0;
}
Code:
$ g++ -o hello hello.cpp
$ ./hello
Hello World
$
 
Old 07-23-2005, 05:49 PM   #13
mlp68
Member
 
Registered: Jun 2002
Location: NY
Distribution: Gentoo,RH
Posts: 333

Rep: Reputation: 40
Actually, this is still not quite right. The right way is

Code:
cout << "Hello World!" << endl;
Doesn't make a visible difference as long as the output is your screen, but conceptually, it's very different. Here you tell the cout object that this output line ends here, instead of adding just a newline character. You actually call a member function of the cout object saying, "ok, I want that output line to end here, do whatever it takes to make it happen". And that can be different things depending in what environment you are using it. Also, for different types of standard output it may vary. Finally, advanced software packages often modify the behavior of the cout and similar objects to direct certain types of output elsewhere, and this relies on calling the member function proper.

Happy C++'ing,

mlp
 
Old 07-24-2005, 04:08 AM   #14
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Hi,

I just wanted to say that I didn't want it to be a sticky as an example of a "Hello World" program. Actually I always though that the title was probably the only bad thing about the thread. I wanted it because it happens very often that people in this forum has compilation problems in C++ because they do not know about the ANSI standard so they use wrong filenames for inclufying files, and the same happens with "using namespace std". I agree that this is not only a C++ forum, though, so I will not bother a moderator.

Regards!
 
  


Reply



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
Hello World! StephenCM LinuxQuestions.org Member Intro 9 01-14-2005 12:59 PM
A whole new world 8oluf7 LinuxQuestions.org Member Intro 2 01-08-2005 06:36 AM
Hello World! professor_x_00 LinuxQuestions.org Member Intro 5 03-24-2004 05:51 AM
world writeable files will not stay world writeable antken Mandriva 1 03-02-2004 05:04 PM
Hello World! flaw Linux - Newbie 6 09-19-2002 02:54 AM

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

All times are GMT -5. The time now is 05:18 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