New and need just a few pointers C++ in OSX

Discussion in 'Mac Programming' started by strangedaysuk, Jun 5, 2010.

  1. strangedaysuk macrumors newbie

    Joined:
    Jun 5, 2010
    #1
    Hi..

    Please can you point me out a bit here. Sorry if its obvious to those in the know but I am lost even with searching loads of forums.

    I have started to learn c++ and at the moment I am using OSX. Now it might be a mistake as I think I have found life much easier in Windows.

    I have downloaded the Xcode, and through recommendations have also used Eclipse.

    There are many Hello World programs around the net but for some reason anything compiling with <iostream> fails

    ie:

    #include <iostream>
    using namespace std;
    void main()
    {
    cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl; }


    This is the standard C++ hello world program but wont compile on a mac??? Using any of the ways I have tried. Either in Eclipse, using Xcodes interface or through command line using the "C++" command.


    However this works:



    #include <stdio.h>

    int main(void) { printf(\"Hello world\"); return 0; }




    Now I am new at this but I cannot find sensible answers around when others ask about this problem other than people suggesting stuff is either missing or
    odd. But this would be the same situation for any noobie learning to program on a mac currently as my install of this stuff is all fresh.

    What I am concerned with is I want to get a book soon to dive into this topic and spend time learning the code properly but if the basic building blocks of C++ are somehow missing in Xcode I need to consider learning on a Windows based PC instead, which I was hoping would not be the case.

    Can anyone clear this up for me.
     
  2. oculus42 macrumors 6502

    Joined:
    Dec 9, 2002
    Location:
    Maine
    #2
    What's not working is you're in C, not C++ on the Mac. stdio.h & printf are C, iostream & cout are C++.

    When you create a project in Xcode, make sure you change the type from C to C++.
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    I compiled it here without problems in the terminal, so you have probably chosen the wrong language in Xcode. You will get an error if you don't use 'int main' and 'return 0' BTW, so your example can't be that standard.
     
  4. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #4
    Does your C++ source file have a ".cpp" extension? If not, then that is your problem.
     
  5. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    Added the int and the return, but it worked here.

     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #6
    Two points:
    1. When you get errors, post the actual error text.
    2. When you use the command line, post your actual commands.

    In general, you should also post the exact name of the file (which should also be in any command line), because development tools often use the extension to infer what language to compile it with.

    For example, when I put your C++ code into a "hello.cpp" file and compile it with this command:
    Code:
    gcc hello.cpp
    
    it produces this error message:
    Code:
    hello.cpp:3: error: '::main' must return 'int'
    
    This message is telling you that the main function must return an int type.


    I also pasted your C code, which you said worked, into a "hello.c" file and compiled it as:
    Code:
    gcc hello.c
    
    which produced this error message:
    Code:
    hello.c:3: error: stray '\' in program
    hello.c:3: error: missing terminating " character
    hello.c:3: error: parse error at end of input
    
    I don't know what to conclude from this, except that what you posted isn't actually C. (I could tell without compiling it that it wouldn't work, but it's always best to check what the compiler thinks by actually compiling.)

    Maybe you're doing something different when you compile your code that somehow makes it compile. Or maybe the code you posted isn't the actual source code in the file. Never retype source code into posts; always copy and paste it exactly as it is.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #7
    It's a "Microsoft standard", meaning it's what Visual Studio does. It apparently also appears in a lot of books.

    http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

    http://users.aber.ac.uk/auj/voidmain.shtml

    Find more by googling: c++ void main
     
  8. GFLPraxis macrumors 604

    GFLPraxis

    Joined:
    Mar 17, 2004
  9. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #10
    Interpreting the hex values as ASCII, it produces:
    Code:
    :(!:
    c99,
    sch.
    
     
  11. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #11
  12. strangedaysuk thread starter macrumors newbie

    Joined:
    Jun 5, 2010
    #12
    Thanks all for your help.

    I went into Xcode and realised that to build a command line utility you can choose between C, C++ etc......

    So you were all spot on. Compiling from the command line didnt work but maybe I am using the wrong command line instructions.

    Can you do a plain .txt file save with the correct extention and make this work?

    again thanks I know where help is!
     
  13. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #13
    Yeah, just end the filename with .cpp and it should work. To compile a file named test.cpp from the command line try this:

    Code:
    g++ test.cpp -o test
    
    This will create an executable named test. You can omit the -o flag and output name, you will then get a file named a.out that will be your executable.
     

Share This Page