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?
Thanks!