PDA

View Full Version : Problems compiling my own programs



TuxCymraeg
29th June 2006, 09:03 AM
I have just purchased a 'Sams Teach Yourself C++ for Linux in 21 Days' and am just starting to work through it. I am trying to compile the following program (The infamous 'Hello World!' starting program!) but get errors. I managed fine on Windows with the same program when I played with C++ on that a few months back but am having issues on Linux. I'm using g++ to compile. Any help appreciated

CODE


# include <iostream.h>

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

COMPILER COMMAND

g++ lst01-01.cxx -o lst01-01

ERROR MESSAGE

In file included from /usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/backward/iostream.h:31,
from lst01-01.cxx:1:
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/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 <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
lst01-01.cxx:7:2: warning: no newline at end of file

markkuk
29th June 2006, 09:17 AM
Looks like your book is obsolete (by several years) and uses the old pre-ANSI C++ standard version of the language. Return the book or throw it away, and get something more current instead. The standard C++ hello world program is:

#include <iostream>

int main()
{
std::cout << "Hello World!" << std::endl;
}

Remember that there must be a line feed after the final "}".

TuxCymraeg
30th June 2006, 12:48 AM
Cheers for that! It compiles it without a problem; only thing now is when I type in lst01-01 to run the file (as directed in the book!) I get a 'command not found' error. Never mind I'll get it working even if I'm on it all night!

Incidentally the book was published in 2000 so it is a little old!! Never mind it gives it more of a challenge working around it!!

I did find something online that said to change the code as below but haven't tried it yet!

#include <iostream>
using namespace std;

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


Once again many thanks

Chris

TuxCymraeg
30th June 2006, 01:27 AM
Finally got it working! I did ./<program name> and it worked. I'd left the . (the dot that is!) on the beginning of the command. My mistake!

Doesn't it make you feel good when you solve it yourself!?

Chris

ibbo
30th June 2006, 02:05 PM
#include <iostream>
using namespace std;

so you can simply use cout << and cin >> respctively without the need to scope it I.E std::cout.
Thats the norm these dyas I beleive.

Its also pretty good feeling to solve a problem thats split between several of you. Wait till you get into full swing OO and you get to code 1 part of something. Fantastic when it all comes together and works (granted not always 1st time).

Ibbo