Here's how to install Xcode on a Mac and create and run a simple C++ program. Xcode is an integrated development environment that uses the clang++ compiler to compile your programs.
Xcode can be downloaded for free from the Mac App Store or from
https://developer.apple.com/downloads/.
Xcode 16.3 is available only for macOS Sequoia 15.5 or later.
Xcode 16.2 is available only for macOS Sonoma 14.5 or later.
Xcode 15.4 is available only for macOS Sonoma 14.0 or later.
Xcode 15.2 is available only for macOS Ventura 13.5 or later.
Xcode 14.3.1 is available only for macOS Ventura 13.0 or later.
Xcode 14.2 is available only for macOS Monterey 12.5 or later.
Xcode 13.4.1 is available only for macOS Monterey 12.0 or later.
Xcode 13.2.1 is available only for macOS Big Sur 11.3 or later.
Xcode is designed to support developers of large systems, so to use it as we will initially to write simple one-file programs may seem like overkill. However, over time you will come to appreciate some features (the debugger, especially).
Here is a way to set up an Xcode project to compile and run a simple one-file program. A number of these steps may have alternate ways to achieve the same end.
Start up the Xcode IDE (Integrated Development Environment).
Select File / New / Project.... You will be asked to choose a template for your new project. Select the macOS tab (or OS X), and in the Application group, select Command Line Tool. Click Next.
Pick a name for your project as the Product Name, such as "hello". For Organization Name, anything will do. For Organization Identifier, something simple like "UCLA" is good. For Language, select C++. Click Next.
Select the folder in which you want your project folder to be created. Click Create.
In the left frame, click once on the name main.cpp to open
the file, which already has some sample code in it. Click once again on the
name to rename the file to what you want it to be, such as
hello.cpp. Click outside the text box to effect the name
change. Replace the sample code in the edit frame with:
#include <iostream>
using namespace std;
int main()
{
cout << "Hey, this really works!" << endl;
}
From the main menu, select View / Debug Area / Activate Console.
In the upper left corner, click the Run right-facing triangle to build and run your program.
If your program has any compilation or link errors, you'll be notified. Click on the little red octagon icon in the status line near the top of the window to see the errors. Fix the errors. Go back to step 7.
If your program built successfully, your output will appear in the Output
frame (bottom right below the edit frame). The line Program ended with
exit code: 0 is produced by the Xcode environment, not by you; we
will never consider it to be part of the output of your program.
When running a program that takes input from the keyboard, don't type arrow keys in the Output frame — they send strange characters to your program and may even appear to reorder some of the characters delivered to the program.