Running Code Locally
Authors: Benjamin Qi, Hankai Zhang, Anthony Wang, Nathan Wang, Nathan Chen
Prerequisites
Options for running your language of choice locally.
Which text editor or IDE should I use?
Depends on your personal preference. Try multiple and see which one you like best.
If you're just starting out, it's easier to begin by running code online and worry about running locally later.
Using an IDE
Resources | |||
---|---|---|---|
IOI | for reference, software you can use at IOI |
Please let us know if you have trouble with installation!
C++
Resources | |||
---|---|---|---|
Geany | Lightweight, frequently used at IOI. | ||
Microsoft | More lightweight than Visual Studio Community, requires some configuration. See PAPS 2.1 and the docs for C++ setup instructions. | ||
Code::Blocks | Windows & Linux. Apparently was unstable at IOI. | ||
Apple | Mac. | ||
Jetbrains | Requires a license, but free for students. |
Java
It can be useful to use a Java IDE to take advantage of the powerful debugging features in Java.
Resources | |||
---|---|---|---|
Jetbrains | free | ||
Eclipse | free | ||
BlueJ |
Python
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 IDE/text editor. However, feel free to use another editor or IDE if you want to.
Using Command Line
C++
You can run your C++ programs from the command line and use a text editor of your choice.
Text Editors
Resources | |||
---|---|---|---|
Fast, lightweight. 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 | |||
From the makers of Github. |
Vim is probably the easiest way to print syntax-highlighted code on Mac, see the response to this post.
Sublime Text Notes (Ben)
- I prefer the Mariana color scheme in place of the default.
- open command palette (Cmd-Shift-P) -> change color scheme
subl
symlink- Using
/usr/local/bin/subl
instead of~/bin/subl
worked for me on OS X Mojave.
- Using
- Package - Sublime Linter (GCC)
- highlights compilation errors and warnings from
-Wall
- can change compilation commands in
linter.py
- highlights compilation errors and warnings from
- Package - Fast Olympic Coding (I don't use)
- test manager can be useful
- linting is covered by the above
- stress testing is covered in Debugging
- can't get debug to work
Further Instructions
See this module for information about installing, compiling, and running C++ from the command line.
Including <bits/stdc++.h>
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.
Resources | |||
---|---|---|---|
SO | wow, people are really mad about this! | ||
SO |
If you install GCC as described later in the module linked above, then you should be good to go.
OS-Specific Recommendations
This section is not complete.
Linux
Windows
Geany or CodeBlocks?
Mac
XCode?
Using <bits/stdc++.h>
Without Installing GCC
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. However, this is not recommended.
Resources | |||
---|---|---|---|
SO | solutions that may or may not work |
Java
First, download the JDK. Then, test that you can use the right commands.
On Windows, open cmd
and type the following command into the prompt:
java
If you get the below result, you may have to add the JDK to your PATH variable.
'java' is not recognized as an internal or external command, operable program or batch file
Otherwise, you're probably good to go.
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:
1public class Main {2 public static void main(String[] args) {3 System.out.println("Hello World!");4 }5}
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
Download Python through the official website. On Windows and Mac, you can download executable files directly; on Linux, you must either download the Python source code and compile from source, or obtain Python through your package manager.
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, running the following in Terminal on my computer produces:
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!