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
Open the Terminal app from Spotlight
Press Cmd+Space, type
Terminaland press Enter. It opens a window where you type commands.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.Fig. 1. The window that installs the command line tools.
Check that
g++is foundRun
g++ --version, which prints a line that starts withApple clang version: on a Mac, the commandg++runs Clang, Apple's C++ compiler.Fig. 2. The answer of
g++ --version.
The editor
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
.dmgfile, and in the window that appears drag Visual Studio Code onto the Applications folder.Fig. 3. Drag Visual Studio Code onto Applications.
Start VS Code
Press Cmd+Space, type
Visual Studio Codeand 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.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.Fig. 4. The C/C++ extension in the search results.
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.Fig. 5. The new folder
cpin the home folder.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 |
| nothing, unless there are errors or warnings |
Run |
| 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.
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
9Run ./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.