c++xcodemacosgcc

Using GCC through Xcode to compile basic programs


So, I'm a brand new CS student, on a Mac, and I'm learning C++ for one of my classes. And I have a dumb question about how to compile my super basic C++ program.

I installed Xcode, and I'm looking through the documentation to try and figure out how to use it (and I highly suspect it's extremely overpowered for what I'm doing right now) and eventually end up going into Terminal and going "gcc [filename]". And I've got a screen full of text that starts with "Undefined Symbols", and goes on about trying to reference things, so I'm wondering if I didn't hook up something somewhere, especially as when I'm actually in Xcode with a C++ program open, most of the menu items are greyed out.

So. In really really basic terms. What did I miss doing, and how do I fix it? Is there a basic guide to Xcode? Most of the documentation is aimed at real developers, and I'm totally missing a lot of what is being assumed.


Solution

  • If XCode is installed then everything is set up correctly.

    If you typed gcc on the command line then you invoked the 'C' compiler (not the C++ compiler). Usually this does not matter as GCC compensates by looking at the file extension. But what does matter is that it does not invoke the linker with the correct C++ flags.

    What you should do (from the command line) is use g++

    g++ <fileName>.cpp
    

    By default the output file is a.out and placed in the same directory.
    g++ has a flag to specify a different output name -o

    g++ -o <outputName> <fileName>.cpp