Setup on Windows
This page helps you install GCC, the compiler the judge uses, and the editor VS Code on Windows, 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. You need 64-bit Windows 10 or later.
The compiler
Download the GCC compiler from WinLibs
On winlibs.com, find the release marked LATEST with the UCRT runtime. Under Win64, take the Zip archive without LLVM/Clang/LLD/LLDB, a file named
winlibs-x86_64-….zip.Fig. 1. The LATEST release on winlibs.com.
Extract the archive to
C:\Right-click the downloaded file, select Extract All, set the folder to
C:\and select Extract. Extracting takes from a minute to several minutes, and you get the folderC:\mingw64.Add the GCC folder to Path
Pathis the list of folders where Windows looks for a command. Open Settings from the Windows search bar and search for Edit environment variables for your account. Under User variables, selectPath, then Edit, then New, addC:\mingw64\binand close both windows with OK.Fig. 2. Adding
C:\mingw64\bintoPath.Check that
g++is foundFind Command Prompt in the Windows search bar, open a new one and run
g++ --version, which prints a line that ends with the version number, such as16.2.0. A Command Prompt readsPathonly when it opens, so one that was already open says thatg++is not recognized.
The editor
Download and install VS Code
It is on code.visualstudio.com. Leave the installer's settings as they are. On its first start VS Code offers to sign in: select Continue without Signing In, then Get Started.
The installer's last page starts VS Code for you, and a VS Code started that way may not see the new
Path. Close it and open it again from the Windows search bar.Add the C/C++ extension
In VS Code, open the Extensions view with Ctrl+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 then suggests the C/C++ Extension Pack, which you don't need.Fig. 3. The C/C++ extension in the search results.
Open a folder for your solutions
Make a folder
C:\cpin File Explorer. In VS Code, select File > Open Folder and choose it. 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.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.
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << '\n';
return 0;
}Select the Run button, the triangle in the top right corner of the editor. The first time, VS Code shows a list of compilers: choose C/C++: g++.exe build and debug active file. It is the second line of the list: the first one, with gcc.exe, is for the C language.
Fig. 4. The list of compilers after the Run button.
VS Code compiles the file into a program, solution.exe, and starts it. The panel at the bottom opens on the Debug Console tab, so select the Terminal tab next to it. The terminal shows the long command that started the program, and then nothing: cin reads what is typed in the panel, so the program waits for the input. Click in the panel and type 12, Enter, 9, Enter, and the program prints 21.
VS Code keeps your choice in a new folder, .vscode, which you leave as it is. If the file has an error, the compiler's message appears in the panel in place of the program. GCC's errors look like the judge's compilation errors.
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, and it needs commands that you type yourself.
Set up the terminal once: open the Command Palette with Ctrl+Shift+P, run Terminal: Select Default Profile and choose Command Prompt. Then choose New Terminal in the Terminal menu, which opens a terminal in C:\cp.
Create input.txt next to solution.cpp and paste the example input exactly as the statement shows it:
12
9Stage | Command | What you see |
|---|---|---|
Compile |
| nothing, unless there are errors or warnings |
Run on the example |
| the program's output, |
Fig. 5. Compiling and running in the terminal.
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.exe. GCC and the program read the files as they are saved on disk, so save both files with Ctrl+S before you compile, or turn on Auto Save in the File menu.
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.
Common mistakes
The wrong folder in Path
If g++ is not recognized even in a new Command Prompt, the Path entry must be C:\mingw64\bin, the folder that holds g++.exe, not C:\mingw64. VS Code also reads Path when it starts. If its terminal does not recognize g++, or the #include line is underlined in red, close VS Code and open it again.
The antivirus blocks the program
An antivirus can take a new solution.exe for a threat: the file disappears, or running it says Access is denied. Add the folder C:\cp to the antivirus exclusions.
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. Name files without spaces, such as solution.cpp, and keep them in a folder like C:\cp.
Compiling while the old program still runs
GCC can't replace a solution.exe that is still running, often waiting for input in another terminal, and reports cannot open output file solution.exe: Permission denied. Stop it with Ctrl+C or close its terminal. A program started with the Run button stops with the red square at the top of the VS Code window.