Can't compile Hello world app in Xcode (First foray into mac coding)

Discussion in 'Mac Programming' started by NightLord, May 14, 2006.

  1. macrumors regular

    Joined:
    Dec 25, 2005
    #1
    I am using a book called "Teach yourself C++" and want to use its first example piece of code to create a basic Hello World app. However, I can't build it, as the Build and Build and go buttons are greyed out.

    Here is the code (Sans the irrelevant comments at top):


    Code:
    #include <iostream>
    
    int main();
    int main()
    {
    std::cout << "Hello World!\n";
    return 0;
    }
    Does anyone know why it won't work? The code is copied perfectly from the book. Thanks alot.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Did you create a new project or just open the file? Unless you start a project you can't use XCodes compile button. You could just use the command line for something this simple though.

    The project type you want is C++ Tool under Command Line Utility
     
  3. thread starter macrumors regular

    Joined:
    Dec 25, 2005
    #3
    I chose "Make empty project" and added an empty Cpp file
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That's probably not the easiest way to go (my suggestion is). Does your empty project have any Targets. Without at least one Target you can't compile...
     
  5. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    Yeah, something wrong with your project setup, there is nothing wrong with the code. I compiled and ran it in Terminal, no problems. As Robbie says, it's probably much easier to do small, text-only projects like this in TextWrangler/Terminal.
     
  6. macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #6
    Here's the easiest way to start a C++ Project in Xcode:

    1. Start Xcode, and go to File > New Project > C++ Tool

    2. Name/save it

    3. Open "main.cpp" and start coding

    4. Click "Build and Go" and your project will be compiled

    Barring some gross oversight, this should work. :D
     
  7. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #7
    I don't know what book you are reading but that's bad coding practice to declare int main(); when int main is the executing function.

    EDIT: That's just what I was taught.
     

Share This Page