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

NightLord

macrumors regular
Original poster
Dec 25, 2005
162
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,643
61
Harrogate
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,643
61
Harrogate
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...
 

HiRez

macrumors 603
Jan 6, 2004
5,829
1,777
Western US
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.
 

steelphantom

macrumors 6502a
Oct 15, 2005
555
1
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
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,281
126
Nowheresville
NightLord said:
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.
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.