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

  1. 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.

    The WinLibs release list: the release marked LATEST, with the 7-Zip and Zip archive links of its Win32 and Win64 lines.

    Fig. 1. The LATEST release on winlibs.com.

  2. 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 folder C:\mingw64.

  3. Add the GCC folder to Path

    Path is 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, select Path, then Edit, then New, add C:\mingw64\bin and close both windows with OK.

    Settings with Edit environment variables in its search field; over it the Environment Variables window, and over that the Edit environment variable window with a new line, C:\mingw64\bin, under the line that was there before.

    Fig. 2. Adding C:\mingw64\bin to Path.

  4. Check that g++ is found

    Find 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 as 16.2.0. A Command Prompt reads Path only when it opens, so one that was already open says that g++ is not recognized.

The editor

  1. 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.

  2. 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.

    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. 3. The C/C++ extension in the search results.

  3. Open a folder for your solutions

    Make a folder C:\cp in 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.

  4. 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.

The list VS Code shows after the Run button: the gcc.exe line first, the g++.exe line under it.

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
9

Stage

Command

What you see

Compile

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

nothing, unless there are errors or warnings

Run on the example

solution.exe < input.txt

the program's output, 21

The Terminal panel of VS Code, a Command Prompt in C:\cp: the compile command, then the run command and under it the output 21.

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.

Was this page helpful?