Setup on Linux

This page helps you install GCC, the compiler the judge uses, and the editor VS Code on Ubuntu or Debian, 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 a terminal for typing commands

    Press Ctrl+Alt+T, or find Terminal among the applications.

  2. Install GCC from the system's packages

    Run sudo apt update, then sudo apt install g++: the first command asks for your password, and the second asks you to confirm with Y. On Fedora the command is sudo dnf install gcc-c++, and on Arch it is sudo pacman -S gcc.

  3. Check that g++ is found

    Run g++ --version, which prints a line that ends with the version number, such as 15.2.0.

    A terminal window: the last lines of the installation, then the command g++ --version and its answer, g++ (Ubuntu 15.2.0-16ubuntu1) 15.2.0.

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

The editor

  1. Download and install VS Code

    From code.visualstudio.com, download the .deb file. In the terminal, run cd ~/Downloads, then sudo apt install ./code_*.deb, and confirm with Y. The installer then asks whether to add the Microsoft repository, which keeps VS Code updated: press Enter for Yes. The notice at the end that mentions Permission denied is not an error.

    A terminal window with a grey box, Configuring code: Add Microsoft apt repository for Visual Studio Code? The answer Yes is selected.

    Fig. 2. The installer's question about the repository.

    Open VS Code: press the Super key (the one with the Windows logo), type Visual Studio Code and press Enter. On its first start VS Code offers to sign in: select Continue without Signing In, then Get Started.

  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

    In VS Code, select File > Open Folder. The window that opens shows your home folder: select the new folder button at its top, name the folder cp, select Create, then Select.

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

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

    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;
}

In VS Code, choose New Terminal in the Terminal menu. In a narrow window the menu bar ends with ···, and the Terminal menu is under it. 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. GCC'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.

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.

The Terminal panel of VS Code in the folder cp: the compile command, then ./solution < input.txt and under it the output 21.

Fig. 5. Compiling and running in the terminal.

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 Ctrl+S before you compile, or turn on Auto Save in the File menu.

Common mistakes

Running the program without ./

The command solution answers solution: command not found, 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 cannot find my: No such file or directory. Give files names without spaces, such as solution.cpp, to avoid this.

Was this page helpful?