Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Russell Quinn

macrumors newbie
Original poster
Dec 3, 2009
5
0
I've not programmed in ages. But I started once again this time on a mac. Installed gcc and wrote some c++ code and got this.

hi.c:2:22: error: iostream.h: No such file or directory

I looked in the book and it seems I wrote it correctly. Could it be a path issue? If so/not could someone pointing me in the right direction.
 
previous I had a line that said cout << "hi"; but I cut it when trying to solve the iostream.h issue.

#include <iostream.h>

void main ()
{




return 0;
}
 
previous I had a line that said cout << "hi"; but I cut it when trying to solve the iostream.h issue.

#include <iostream.h>

int main ()
{




return 0;
}

Well, you said that main returned nothing (void) and yet you returned something. Either remove return 0; or define main with a return type of int.
 
I don't know about gcc, as I mainly used the Microsoft VS C++ compiler.. But try just #include <iostream>, no .h

edit: too late, mathew beat me to it.
 
I've not programmed in ages. But I started once again this time on a mac. Installed gcc and wrote some c++ code and got this.

hi.c:2:22: error: iostream.h: No such file or directory

I looked in the book and it seems I wrote it correctly. Could it be a path issue? If so/not could someone pointing me in the right direction.

Installed gcc? Huh? Do you mean you installed Xcode, or do you mean you actually went and found gcc itself and installed it? You should just install Xcode. It includes the proper Apple developer tools--including gcc.

Next, try:

#include <iostream>

The method you used is deprecated. It should have worked, but it *is* technically deprecated.

I should probably also point out that C++ received a major overhaul in 1998, so if you learned C++ prior to that, you have some catching up to do (e.g. namespaces, virtual functions). It's also nearing yet another overhaul (currently being referred to as C++-0x). It will take a while for compilers to catch up, but it's something you should be aware of.

also note that due to the namespace changes in C++-98, you no longer:

Code:
cout << "hello world!!";

Now, you:

Code:
std::cout << "hello world!!";

or

Code:
using namespace std;
cout << "hello world!!";
 
hi.c:2:22: error: iostream.h: No such file or directory

While I don't necessarily disagree with the others above, IMO, that error sounds like you are trying to compile a C++ program using a C compiler, considering that the name of your file is "hi.c" and not "hi.cpp".

As others have said, you should be doing "iostream" instead of "iostream.h". As mentioned above though, using iostream.h should have worked although it has been deprecated (I just tried it and I got a warning saying that. if I compile doing "gcc foo.c", I get the same error).

So, use "include <iostream>", get into the habit of using ".cpp" or some other c++ file extension (see the gcc man page for a listing of all file extensions it associates with c++ code) for your C++ code and use "g++" instead of "gcc" when compiling. If you compiled your code by doing "make hi", then the appropriate compiler is chosen based on your file extension.

PS I'm assuming you meant that you installed XCode and not gcc directly. You should have installed xcode, even if you want to just use the compiler from the command line like you are doing.
 
Yeah, iostream.h is deprecated. I've run into that while working from an older C++ book, as well as lack of the declaration:

Code:
int main (int argc, char * const argv[])

Code w/o this declaration (as the examples in my book) fails to compile. Putting it in makes it work, though. :cool:

[edit] This works as well:

Code:
int main()
 
And on both Mac OS and iPhone …

Code:
int main(int argc, char* argv[], char* envv[])

… where 'envv[]' is a NULL terminated array of string pointers used to pass the exec's environment variables.
 
Thanks
I guess I've been gone a long time. C++ has changed--I'll see if those help. And yes I did install xcode.
 
I've been using gcc which maybe be just c. (I enter that command by memory hoping it to be the compiler, I recalled using gcc from my Linux days). What would be the C++ compiler if that's not it? Could anyone tell me of a good doc sight for xcode that's for newbies?
 
I don't believe it actually matters anymore whether you use gcc or g++. I believe it picks the language initially based on the file extension as opposed to the actual command name used to invoke the compiler.
 
I get a whole string of errors if I attempt to compile a .cpp with the gcc tool. Works fine with g++.
 
I don't believe it actually matters anymore whether you use gcc or g++. I believe it picks the language initially based on the file extension as opposed to the actual command name used to invoke the compiler.

Well, sort of. If you call "gcc" with a c++ source file, assuming the extension you used is one of the c++ extensions it recognizes, it will indeed run it through the c++ compiler. The difference is that it will not link it against the standard c++ library whereas g++ will. Thus, you will probably get a bunch of link errors if you attempt to compile c++ code with gcc. The OP's problem is that he is not only using gcc, but he is using gcc with a .c extension (in addition to adding the ".h" to the include statement).

So, if the OP just renamed his file to hi.cpp and still used gcc, he would no longer get the error about the header file but would most likely instead get a bunch of errors at link time.

In short, if one wants to compile c code, they should use "gcc". If they want to compile c++ code, they should use "g++".

ps: all of this is explained in the gcc man page :)
 
Thanks guys. The code worked. Now I've gotta relearn c++. When you said man page it clicked I remembered.
man gcc.
Now I'll retry being a computer nerd. ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.