Setup on macOS

This page helps you install a C++ compiler and the editor VS Code on a Mac, and then run a solution on a problem's example. You can start without it: the editor on a problem's page is enough for your first solutions, and other systems have their own pages.

The compiler

  1. Open the Terminal app from Spotlight

    Press Cmd+Space, type Terminal and press Enter. It opens a window where you type commands.

  2. Install the command line tools

    Run xcode-select --install, and a window asks whether to install the command line developer tools: select Install, then Agree under the license. The window can open behind the Terminal window. Downloading and installing takes several minutes, until the window says that the software was installed.

    A Terminal window with the command xcode-select --install; next to it a window asks whether to install the command line developer tools, with the buttons Cancel and Install.

    Fig. 1. The window that installs the command line tools.

  3. Check that g++ is found

    Run g++ --version, which prints a line that starts with Apple clang version: on a Mac, the command g++ runs Clang, Apple's C++ compiler.

    The Terminal window: the command g++ --version and its answer, which starts with Apple clang version 21.0.0.

    Fig. 2. The answer of g++ --version.

The editor

  1. Download and install VS Code

    Download it from code.visualstudio.com. If Safari asks whether to allow downloads from the site, select Allow. Open the downloaded .dmg file, and in the window that appears drag Visual Studio Code onto the Applications folder.

    The window of the downloaded file: the Visual Studio Code icon on the left and the Applications folder on the right.

    Fig. 3. Drag Visual Studio Code onto Applications.

  2. Start VS Code

    Press Cmd+Space, type Visual Studio Code and press Enter. The first time, macOS asks whether you are sure you want to open an app from the internet: select Open. VS Code then offers to sign in: select Continue without Signing In, then Get Started.

  3. Add the C/C++ extension

    In VS Code, open the Extensions view with Cmd+Shift+X, search for C++ and install the extension named C/C++, by Microsoft. It may not be the first one in the list. VS Code later suggests the C/C++ Extension Pack, which you don't need.

    The Extensions view of VS Code with C++ in its search field: a struck-out C++ Intellisense first, then C/C++ by Microsoft with an Install button.

    Fig. 4. The C/C++ extension in the search results.

  4. Open a folder for your solutions

    In VS Code, select File > Open Folder. The window shows your home folder: select New Folder, name the folder cp, and select Open. VS Code opens the new folder in Restricted Mode and says so in a bar at the top of the window: select Manage in the bar, then Trust. If it asks whether you trust the authors instead, select Yes, I trust the authors.

    The Open Folder window on the home folder: the new folder cp is selected above Desktop, Documents, Downloads and the others, with the New Folder button at the bottom left and Open at the bottom right.

    Fig. 5. The new folder cp in the home folder.

  5. Create the file for the solution

    In VS Code's Explorer, the file list on the left, select the New File button and name the file solution.cpp.

The first program

Write the solution of problem 8804, the sum of two integers, into solution.cpp. Clang has no <bits/stdc++.h>, which belongs to GCC, so on a Mac the solution starts with the standard headers it uses, here <iostream>.

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
    return 0;
}

In VS Code, choose New Terminal in the Terminal menu. A terminal opens at the bottom, already in the folder cp, and you type commands in it.

Stage

Command

What you see

Compile

g++ -std=c++17 -O2 -Wall solution.cpp -o solution

nothing, unless there are errors or warnings

Run

./solution

the program waits for the input

In the compile command, -std=c++17 compiles by the C++17 standard, as the judge does. The -O2 flag turns on optimization, -Wall turns on warnings about suspicious code, and -o solution names the program solution. Clang's errors look like the judge's compilation errors.

After ./solution nothing is printed yet: cin reads what is typed in the terminal, so the program waits. Type 12, Enter, 9, Enter, and the program prints 21.

The VS Code window: solution.cpp in the editor, and in the Terminal panel under it the compile command, then ./solution, the typed numbers 12 and 9 and the output 21.

Fig. 6. Compiling and running in the VS Code terminal.

Running on the example from a file

Typing the example every time is slow, so save it in a file, input.txt, and let the terminal give the file to the program as if you typed it. This is called input redirection. Create input.txt next to solution.cpp and paste the example input exactly as the statement shows it:

12
9

Run ./solution < input.txt, and the program prints 21 without waiting. Compare the output with the example's answer character for character, as the judge does. When they match, submit the file. A match only shows that the program works on that example: the judge also runs hidden tests, so a solution can match the example and still get Wrong answer.

The compiler and the program read the files as they are saved on disk, so save both files with Cmd+S before you compile, or turn on Auto Save in the File menu.

Common mistakes

Including <bits/stdc++.h> in the solution

Clang stops with fatal error: 'bits/stdc++.h' file not found, so include the standard headers the solution uses instead; the judge accepts them as well.

Running the program without ./

The command solution answers command not found: solution, because the terminal doesn't look for programs in the current folder, so start it as ./solution.

Spaces in file names

With a file named my solution.cpp, the command g++ my solution.cpp reads two names, my and solution.cpp, and stops with no such file or directory: 'my'. Give files names without spaces, such as solution.cpp, to avoid this.

Was this page helpful?