Extremely Basic XCode / C++ Question re "Hello World" program

I have an extremely basic question. I'm just starting a C++ course, and would like to compile the code on my Mac. From what I've read, XCode is an appropriate tool for that. But I know nothing about either C++ or XCode at this point. (I've looked at the XCode manual, and find it overwhelming).

The code I would like to compile is as follows:

#include <iostream>

int main()
std::cout << "Hello World!\n";
return 0;

When I try to "Build" this in XCode, I get an error saying "Duplicate symbol _main"

I sort of see where this is coming from. Just clicking around, I see that XCode has built a file in this XCode "project" named main.m, and that file contains the following code:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
return NSApplicationMain(argc, (const char **)argv);

So sure enough, there's a "main" in my code, and another one in main.m - but why, and what am I supposed to do about it?

I have gotten this far just by clicking around in XCode and trying to make it work, so the problem might be that the things I am clicking on, before typing in the code, or in trying to execute the code, are not the right things. Here are the things I'm doing in Xcode from the time I start XCode to the time I see the error. Any of these steps might be what is getting me into trouble:

1) In the XCode "splash screen", I click on "Create a new XCode project".

2) I highlight "Mac OS X" on the left, "Cocoa application" on the right, and click "Next".

3)In the "Product Name" field, I type "hello"., and click "Next".

4) On the dropdown menu which appears, I do not check the "Source control" box. Instead, I just click "Create".

5) In the XCode menu, I do "File => New File => C++ File, and click "Next".

6) In the "Save As" field, I type "hello"

7) I type my code in at this point. The file XCode opens for me has some comment lines at the top which I leave alone, and also already has one line which reads:
#include <iostream>
So I don't duplicate that line, but I do type in the rest of my code.

8) I click on the large "Run" button in the top left corner of the XCode window.

9)I see a red error message stating that the build failed.

10) I click on the red exclamation point, and then on the text which appears at the left, saying "Apple Mach-O Linkder (ld) Error".

11) This produces a lot of information, but I think the two most relevant lines in it are:
Command /Developer/usr/bin/clang++ failed with exit code 1
ld: duplicate symbol _main

BTW, I've noticed that a very similar question has already been asked and answered at http://www.cplusplus.com/forum/beginner/5609/
but the answer given there was to "exclude main.cpp from compilation". I guess in my case, it would be "main.m" - but I don't know how to do that, and I am afraid of what I will be messing up if I did do it.

So... thanks if you've read this far. I kind of suspect that my problem is being caused because I'm overcomplicating things in XCode (as in, what do I need an XCode "Project" for anyway? - Why not just a .cpp file?).

My whole question really reduces simply to: What are the things I need to click on in order to type in and execute a simple "Hello World" C++ program in XCode?

Your problem is in step 2. A Cocoa project is an Objective C project. The one you want is the "Command Line Tool" which should be in the "Applications" section I believe (I'm not at my mac at the moment).

Then, from the drop-down box below, choose "C++ stdc++". Then just do exactly the same as you did before - give your project a name etc. This should give you a skeleton Hello World C++ project which should just compile and run out of the box.

Objective C is the OS X and iOS (iPhone/iPad) native language. But even though it's roots are in C, it's a very different language. Don't confuse the two!

You did all the right things attempting to track down the problem. I've been using XCode for some years now, yet it still manages to stump me on the simplest things on occasion.

By the way, are you using XCode 3.6.2 or XCode 4?
Last edited on
Yes, that is much better, thanks! It now compiles, loads, and runs properly!

Could I ask a couple of followup questions though?

1) I've found that XCode is creating a file called "main.cpp", which has a comment in it saying "insert code here", which I have done, and which works. I've done this a few times now, exiting XCode, deleting what I think is the "Project folder" XCode is creating on my desktop, and starting up with a new project name. The funny thing is that, even though I'm trying to start over fresh each time, the code I last typed in is "remembered", and is already there in main.cpp when I start a new project! I guess I am not doing the proper steps to clean up after myself so I can truly start fresh with a new project. Can you tell me how to delete my old project properly?

2) I am fine for now with sticking my code into "main.cpp" - but I expect that, before long, I'll be needing to name my source files something other than "main". For instance, the book that I copied the "Hello World" code out of names the file "hello.cpp". Can you tell me how to name the file my source code is in something other than "main.cpp"?

I am really excited to have found this forum! I can't believe the speed and accuracy of the response! Thanks again!

Oh Yes - almost forgot to say - in response to your question, Lodger, I am using XCode 4.1 on Lion.
Last edited on
Ask away! :)

Empty your trash before creating your new project, that should properly delete everything. XCode will happily compile source files that are in the trash, that's caught me out a couple of times before.

You can call your files whatever you like. Try renaming "main.cpp" to "hello.cpp" from inside XCode! (Just click the file once in the "Groups and files" list. If you rename it in Finder behind XCode's back, it won't know that the file has been renamed and moan like hell at you.

To add new files to your project, right-click (or ctrl-click if you have a single button mouse) on one of the folders "Groups and Files" list on the left, select "Add" -> "New File", select a file type (.h or .cpp), give it a name and you're ready to rock and roll.

XCode can be a bit quirky sometimes but once you get used to it, it's not that bad and is actually quite powerful.

Sorry to ask again, but which version of XCode are you running? I run both XCode 3.6.2 and XCode 4 side by side, but they are slightly different & I don't want to give you incorrect instructions.

Edit: Sorry didn't see your edit! Right, the instructions I've given you are for 3.6.2. "Groups and files" is called something else now I think but it's basically the tree of folders and source files. I'll fire up 4.1 tomorrow if you need anything else (it's late!).

Happy coding :)
Last edited on
Well, I've deleted the old project folder and emptied the trash, but the phenomenon still occurs. Even though I give the new project a brand new name, when I look in the main.cpp file, I see:

// insert code here...
std::cout << "Hello, World!\n";
return 0;

I tried adding a new comment line at the end of this code:
// end of hello world

and found that, after exiting, deleting, & emptying trash, that line did NOT show up - so it doesn't seem to be remembering my new code anymore, but is remembering the original code from earlier, for some reason.

Strange and a bit annoying, but not worth anyone knocking themselves out over, since all I have to do is delete those two lines each time.

As for the file renaming, thanks again Lodger, that worked just as you described. You're a great resource!
Topic archived. No new replies allowed.