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
Open a terminal for typing commands
Press Ctrl+Alt+T, or find Terminal among the applications.
Install GCC from the system's packages
Run
sudo apt update, thensudo apt install g++: the first command asks for your password, and the second asks you to confirm withY. On Fedora the command issudo dnf install gcc-c++, and on Arch it issudo pacman -S gcc.Check that
g++is foundRun
g++ --version, which prints a line that ends with the version number, such as15.2.0.Fig. 1. The answer of
g++ --version.
The editor
Download and install VS Code
From code.visualstudio.com, download the
.debfile. In the terminal, runcd ~/Downloads, thensudo apt install ./code_*.deb, and confirm withY. 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 mentionsPermission deniedis not an error.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 Codeand press Enter. On its first start VS Code 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 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
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.Fig. 4. The new folder
cpin 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.
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 |
| 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. 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
9Run ./solution < input.txt, and the program prints 21 without waiting.
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.