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

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.

Using the Command Line

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"
          |                         ^

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