PrevNext

Running Code Locally

Author: Many

Contributors: Benjamin Qi, Hankai Zhang, Anthony Wang, Nathan Wang, Nathan Chen, Owen Wang, Shourya Bansal, Dong Liu

Options for running your language of choice locally.

If you're just starting out, it's easier to begin by running code online and worry about running locally later. You can find more information about running code online here.

Resources
IOI

for reference, software you can use at IOI

Code Editors vs IDEs

First, you'll need to decide whether to use a code editor such as Sublime Text or an integrated development environment (IDE) such as Visual Studio. An IDE provides many features beyond just a code editor, though as you won't need most of them for competitive programming, a code editor alone may suit your needs. More Info

Which code editor or IDE should I use?

It depends on your personal preference. Try multiple and see which one you like best.

Using an IDE/Editor

C++

If you've only run code online before, it's probably easiest to start by installing Visual Studio Code. Please let us know if you have trouble with this process!

Resources
Microsoft

See PAPS 2.1 and the docs for C++ setup instructions.

Jetbrains

Requires a license, but free for students.

Geany

Lightweight, frequently used at IOI.

Code::Blocks

Windows & Linux. Apparently was unstable at IOI.

Apple

Mac only.

Java

It can be useful to use a Java IDE to take advantage of the powerful debugging features in Java.

Resources
JetBrains

free

Eclipse Foundation

free

King's College London

free

Python

Windows

Python comes with a program, known as IDLE (Integrated Development and Learning Environment), that allows one to write and run Python code. It supports many features, such as syntax highlighting and automatic indentation, that one would normally expect from an code editor.

Alternatively, you can install editing software like PyCharm or VSCode.

Linux

You can use built-in terminal editors, like vim and nano. You can also install editing software like VSCode. If you don't know how to exit, have a look at this link.

Using the Command Line

C++

Alternatively, you can edit your code with an editor of your choice and run your C++ programs via the command line (e.g., Terminal on Mac). See this module for information about installing, compiling, and running C++ from the command line.

Resources

Fast, lightweight, easy to use. Unlimited free evaluation period, though it will repeatedly ask you to purchase a license.

Classic text editor, usually preinstalled on Linux. Also see Neovim, MacVim. Probably the easiest way to print syntax-highlighted code on Mac, see this post.

Sublime Text Notes (Ben)

Including <bits/stdc++.h>

If you have installed g++ as described here, you can use #include <bits/stdc++.h> in place of separately including libraries.

Usage

This is usable with GCC. However, Mac OS X uses Clang while Windows uses Microsoft Visual C++ (MVSC) by default. <bits/stdc++.h> is not a standard header file, so it will not work with the latter two. This is one of the reasons why you should not use <bits/stdc++.h> outside of competitive programming.

Using <bits/stdc++.h> Without Installing g++

If you installed Clang on Mac, then you can download stdc++.h from here and move it into a folder named bits that is located in the same directory as where all other C++ header files are located.

Resources
SO

solutions that may or may not work

Precompiling <bits/stdc++.h>

Including <bits/stdc++.h> may significantly slow down compilation time. Using this trick usually speeds up compilation time to less than a second.

  1. Find out where bits/stdc++.h is located on your machine. To do so, compile your C++ file file.cpp that includes bits/stdc++.h with the following command.

    g++ -H file.cpp

    This will generate a list of included libraries. You should see a path of the form /path_to/bits/stdc++.h near the top of the list.

  2. Precompile the header:

    g++ -add-flags-here /path_to/bits/stdc++.h

    where -add-flags-here includes the compilation flags you normally use (ex. -std=c++17 -O2)

  3. Recompile your C++ file. Should be faster this time since the precompiled header will be used.

    g++ -add-flags-here -Winvalid-pch file.cpp

    The -Winvalid-pch will warn you if the precompiled header is not used. For example, if I precompiled stdc++.h with -std=c++17 but I compiled my C++ program with -std=c++11 -Winvalid-pch, I would get a warning such as the following:

    file.cpp:1:25: warning: bits/stdc++.h.gch: not used because `__cpp_nontype_template_parameter_auto' not defined [-Winvalid-pch]
        1 | #include "bits/stdc++.h"
          |                         ^

Java

Installation

Linux

First, you can check if you have Java already installed by running java --version in the terminal. If it returns something like this:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

That means you have Java installed already. On the other hand, if you get something like this:

Command 'java' not found

You will have to download and install JDK. On most Linux distros, the JDK can be downloaded via the built in package manager.

Ubuntu/Debian

On Ubuntu, Debian, and other Ubuntu/Debian based distos, the JDK can be installed via apt. You can install the Java JDK from the default repositories by running: sudo apt install default-jdk.

After this finishes installing, if you run java --version again, you should get something like above.

Arch

On Arch and other Arch based distos (like Manjaro), the JDK can be installed via pacman. You can install the Java JDK from the default repositories by running: sudo pacman -S jdk-openjdk.

After this finishes installing, if you run java --version again, you should get something like above.

Fedora

On Fedora and other Red Hat based distos, the JDK can be installed via dnf. You can install the Java JDK from the default repositories by running: sudo dnf install java-openjdk-devel.x86_64.

After this finishes installing, if you run java --version again, you should get something like above.

Windows

First, can check if you already have Java installed by running java --version in cmd. If it returns something like this:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

That means that you have Java installed already. On the other hand, if you get something like this:

'java' is not recognized as an internal or external command,
operable program or batch file.

You will have to download and install JDK from here.

After it finishes installing, if you run java --version again, you should get something like above. Note: the installer should automatically add Java into the system PATH, but in the event that it doesn't, you can find more information on how to do that here.

macOS

First, can check if you already have Java installed by running java --version in the terminal. If it returns something like this:

openjdk 11.0.9.1 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

That means that you have Java installed already. On the other hand, if you get something like this:

-bash: java: command not found

You will have to download and install JDK from here.

After it finishes installing, if you run java --version again, you should get something like above.

Compiling and Running

Running a Java file off of the command-line is relatively simple after the JDK is downloaded.

Consider this code of Main.java and assume it is in a file on your computer:

public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Use the cd command on your console to get to the directory that contains Main.java. Then, run the following command to compile the code:

javac Main.java

If it runs successfully, a file called Main.class will show up in the same directory, which can be executed with the next command:

java Main

If everything goes accordingly, the console should output Hello World!.

If you do USACO-style file I/O, meaning that files are in the "local directory", then the files must be located in the same directory as the source code (if you use the above method).

Python

Installation

Windows and Mac

Download Python through the official website.

Linux

If you have apt (the package manager), you can install Python 3 with

sudo apt install python3.8

You can also build from source and install.

Make sure that you are using the correct version of Python. Python 2 is quite different from Python 3 (but parts of the version number beyond 2. or 3. do not matter much). We generally recommend newcomers to use Python 3, but if you are used to programming in Python 2, that is OK too.

The Python version can matter when using the command line to run Python; sometimes, python3 must be used instead of python in order to run Python 3. Try both commands, and pay attention to the version that Python prints to determine if this is the case on your system. For example:

python --version   # prints Python 2.7.13
python3 --version  # prints Python 3.8.1

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext