Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PieceMaker

macrumors newbie
Original poster
May 30, 2008
8
0
Texas
Hey everyone. I recently got a MacBook Pro as a Graduation/B-day present. I enjoy messing with programming and am going to be helping out a friend with C++ this summer. I want to be able to write and compile code on my Mac, so I downloaded and installed Xcode. Then to test it out, I wrote a simple code and tried to compile it. For a step by step guide, here is what I did:

I opened a new project, then selected C++ Tool from the Command Line Utility. Next I named the project Test. I then control-clicked the project and added a new C++ file from the C and C++ drop down list. I then wrote this simple test code in the file:


Code said:
#include <iostream>
using namespace std;
int main()
{
cout << "It works." << endl;
return 0;
}

Finally I tried to build and run it, but every time I attempted to build it, it would say that there were two errors. Each of them said this:

[quote="Error]
Command /Developer/usr/bin/g++-4.0 failed with exit code 1
[/quote]

The compiler then quits. I have no idea why this isn't working, but I'm hoping it's just something simple that I'm overlooking or a simple file I haven't configured. Any help would be much appreciated. Thanks a bunch.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
It is unfortunate that we cannot actually see the error, but a C++ created from the template does already have a main () function, so by adding one you probably have two. So the linker will complain once when it links the x86 version, and again when it links the PowerPC version.
 

PieceMaker

macrumors newbie
Original poster
May 30, 2008
8
0
Texas
Hmm, seeing as I'm not quite sure what you mean, I'll post this screenshot of the image. Seeing as the entire message would be really big, I'll post the condensed version, but if need be I'll take a picture of the whole message.

screenshot.png
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Looks like what was gnasher previously suggested - you have a duplicate symbol for _main.

Post your full code, and post the error again, but make the window wider so we can see the full specific file name(s).
 

PieceMaker

macrumors newbie
Original poster
May 30, 2008
8
0
Texas
Alright, as for the code, to my knowledge that was the full code that I had in my first post. However, I wouldn't be surprised if the project added something that I wasn't aware of.

Now, as for the error message, here it is. I hope it's not too big.

screenshot_full.png
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
As I said, there is a file main.cpp in your project, containing a "main" function. You added another file, with another "main" function. Now you have two, and the linker cannot decide which one you want.

That's what the error message from the linker says: You have two symbols named "_main". One is in Test2.o, which is the object file that you got when compiling Test2.cpp or Test2.c. The other is in main.o, which is the object file that you got when compiling main.cpp or main.c.
 

PieceMaker

macrumors newbie
Original poster
May 30, 2008
8
0
Texas
Ah, it's now compiling. Thank you all very much. Hopefully this will be the last Xcode question for a while.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.