Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I do not want to discuss about C++ or any programming language!I just want to know what am i doing wrong with linux ubuntu about compiling helloworld.cpp!

I am learning C++ so my steps are:

open hello.cpp in vim and write this

#include <iostream.h>
int main()
    cout << "Hello World!\n";`
    return 0;

So, after that i tried in the terminal this

g++ hello.cpp

AND the output is

hello.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.

What do you suggest? Any useful step by step guide for me?Thanks!

share|improve this question
In case anyone is considering voting to close this question as off-topic: This really is a platform-specific issue, because the most popular C++ compiler for Windows (Microsoft Visual C++) accepts this syntax, but most other compilers (including g++ in Ubuntu) do not accept it. –  Eliah Kagan Sep 26 '12 at 19:50
#include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; } this can work... –  user272342 Apr 22 at 7:47

2 Answers 2

up vote 21 down vote accepted

You should use #include <iostream>, not iostream.h; the .h form is very old and deprecated since years.

You can read more than you probably want to know on the .h vs non-.h forms here: http://members.gamedev.net/sicrane/articles/iostream.html

(Plus, you should write std::cout or have a line using namespace std; otherwise your next error will be about the compiler not finding a definition for cout.)

share|improve this answer
@gabriel C++ has a long history, dating back to the mid-1980. There are now several published standards, the latest of which (C++11) has been released earlier this year, and the previous one (the one that all compilers follow today) dates back to 2003 (C++03). The guide you linked seems to be much older than that though, judging from the compiler software it mentions (according to Wikipedia, Borland Turbo C++ was discontinued in 1994). If you pick a tutorial following the C++03 standard, your code will work on any Linux distribution available today. –  Riccardo Murri May 3 '12 at 14:29
@gabriel Looks like your question has been already answered on StackOverflow: stackoverflow.com/questions/909323/… –  Riccardo Murri May 3 '12 at 15:31
You may also need: sudo apt-get install build-essential –  david6 May 4 '12 at 9:01
@david6: I have build-essential installed but there's no iostream.h on my system. Actually, C++ header files are installed by libstdc++-dev, which is required by the C++ compiler package g++, which in turn is required by build-essential. So, no, you do not need build-essential to compile simple C++ programs; installing the g++ package is enough. –  Riccardo Murri May 4 '12 at 10:13
For just getting cout to work without the std:: prefix, using std::cout; is preferable to using namespace std;. Even multiple using statements (without namespace) are generally better, since you don't bring in identifiers from the std namespace that you don't want and might conflict with names in your own program. –  Eliah Kagan Sep 26 '12 at 19:26

You should change iostream.h to iostream. I was also getting the same error as you are getting, but when I changed iostream.h to just iostream, it worked properly. Maybe it would work for you as well.

In other words, change the line that says:

#include <iostream.h>

Make it say this instead:

#include <iostream>

The C++ standard library header files, as defined in the standard, do not have .h extensions.

As mentioned Riccardo Murri's answer, you will also need to call cout by its fully qualified name std::cout, or have one of these two lines (preferably below your #include directives but above your other code):

using namespace std;
using std::cout;

The second way is considered preferable, especially for serious programming projects, since it only affects std::cout, rather than bringing in all the names in the std namespace (some of which might potentially interfere with names used in your program).

share|improve this answer
Great answer. In answering @gabriel's question you've resolved a couple side confusions I've had for years. –  isomorphismes Feb 12 '13 at 20:33

protected by Community Oct 3 at 15:17

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.