Xcode Compiling Problem

Discussion in 'Mac Programming' started by PieceMaker, May 30, 2008.

  1. PieceMaker macrumors newbie

    Joined:
    May 30, 2008
    Location:
    Texas
    #1
    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:


    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.
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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.
     
  3. PieceMaker thread starter macrumors newbie

    Joined:
    May 30, 2008
    Location:
    Texas
    #3
    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.

    [​IMG]
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    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).
     
  5. PieceMaker thread starter macrumors newbie

    Joined:
    May 30, 2008
    Location:
    Texas
    #5
    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.

    [​IMG]
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     
  7. PieceMaker thread starter macrumors newbie

    Joined:
    May 30, 2008
    Location:
    Texas
    #7
    So I can just delete main.cpp and it should work?
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    Or just delete what is in it, put your code in there and delete your file. Keeping the main.cpp file makes it easier to see where the main function is in multi file projects.
     
  9. PieceMaker thread starter macrumors newbie

    Joined:
    May 30, 2008
    Location:
    Texas
    #9
    Ah, it's now compiling. Thank you all very much. Hopefully this will be the last Xcode question for a while.
     

Share This Page