Revisited programming on my Mac

Discussion in 'Mac Programming' started by Russell Quinn, Dec 3, 2009.

  1. Russell Quinn macrumors newbie

    Joined:
    Dec 3, 2009
    #1
    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.
     
  2. VPrime macrumors 68000

    VPrime

    Joined:
    Dec 19, 2008
    Location:
    London Ontario
  3. Russell Quinn thread starter macrumors newbie

    Joined:
    Dec 3, 2009
    #3
    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;
    }
     
  4. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #4
    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.
     
  5. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #5
    Oh jeez, yeah remove that .h

    It should just be <iostream>
     
  6. VPrime macrumors 68000

    VPrime

    Joined:
    Dec 19, 2008
    Location:
    London Ontario
    #6
    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.
     
  7. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #7
    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!!";
     
  8. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #9
    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.
     
  9. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #10
    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()
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    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.
     
  11. Russell Quinn thread starter macrumors newbie

    Joined:
    Dec 3, 2009
    #12
    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.
     
  12. Russell Quinn thread starter macrumors newbie

    Joined:
    Dec 3, 2009
    #13
    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?
     
  13. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #14
    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.
     
  14. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #15
    I get a whole string of errors if I attempt to compile a .cpp with the gcc tool. Works fine with g++.
     
  15. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #16
    To compile C++ applications you should use the command g++, not gcc. :)
     
  16. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #17
    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 :)
     
  17. Russell Quinn thread starter macrumors newbie

    Joined:
    Dec 3, 2009
    #18
    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. ;)
     

Share This Page