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.
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..
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.
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?
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.
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.
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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.