This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/1_Intro/Running_Cpp.md
Benjamin Qi 032c7f39c3 intro
2020-06-24 17:29:24 -04:00

8.3 KiB

id title author description
running-cpp Running C++ Nathan Wang, Benjamin Qi, Anthony Wang Options for running C++ both online and locally.

Please let us know if these installation instructions do not work for you.

Using C++ Online

  • OnlineGDB
    • online compiler with an embedded GDB debugger
    • can be buggy sometimes
    • supports files and file I/O
  • CSAcademy
    • pretty nice (unless you get "Estimated Queue Time: ...")
    • "saved locally" will not save your code if you close the tab, press Command-S to save.
  • Ideone
    • okay ... has the bare minimum you need for running C++
    • make sure your code is not public
    • sometimes erases your code when you first create it (so get in the habit of copying your code first)

You can share code with pastebin or hastebin.

Using an IDE

These often have C++ support already built-in.

Using Command Line

Alternatively, run C++ from the command line and use a text editor of your choice.

Text Editors

  • Sublime Text 3
    • fast, lightweight text editor for Windows, Mac, and Linux
    • Editing Build Settings
      • no need to do this if you just use command line to compile & run
    • FastOlympicCoding Addon
      • see "Debugging" for another way to stress test
    • Sublime Snippets
      • Ben - I use to insert templates
    • Symlink
      • Ben - Using /usr/local/bin/subl instead of ~/bin/subl worked for me on OS X Mojave.
  • Atom
    • another text editor for Windows, Mac, and Linux from the makers of Github
  • Vim
    • classic text editor, usually preinstalled on Mac and Linux, and also available for Windows
    • probably easiest way to print syntax-highlighted code on Mac, see the response to this post

On Linux

GCC is usually preinstalled on most Linux distros. You can check if it is installed with

whereis g++

If it is not preinstalled, you can probably install it using your distro's package manager.

On Windows

MinGW

One option is MinGW.

First, download and run the MinGW installer. Once it's installed, open the MinGW Installation Manager, click on Basic Setup on the left, and select mingw32-gcc-g++-bin for installation.

Adding MinGW to PATH

WSL

Another good option is Windows Subsystem for Linux (WSL) which is what I (Anthony) personally use, although it may be more difficult to properly set up.

VSCode Docs for WSL (difficult for beginners)

Nathan Wang: If you want to code in (neo)vim, you can install WSL and code through WSL bash.

  • Note that WSL has a max stack size of 64MB; I am unsure if this limitation is resolved yet.
  • Note that WSL/vim clipboard integration is imperfect.

On Mac

Clang is the default compiler for Mac OS X, but you should use GCC's g++ since that's what USACO uses to compile your code.

Installation

  1. Open the Terminal application and familiarize yourself with some basic commands.

  2. Install XCode command line tools.

    xcode-select --install
    

    If you previously installed these you may need to update them:

    softwareupdate --list # list updates
    softwareupdate -i -a # installs all updates
    
  3. Install Homebrew.

  4. Install gcc with Homebrew.

    brew install gcc
    

    According to this if brew doesn't seem to finish for a long time then

    brew install gcc --force-bottle
    

    probably suffices.

  5. You should be able to compile with g++ or maybe g++-#, where # is the version number (ex. 9). Running the following command

    g++-9 --version
    

    should display something like this:

    g++-9 (Homebrew GCC 9.2.0_2) 9.2.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

Running With the Command Line

Consider a simple program such as the following, which we'll save in name.cpp.

#include <bits/stdc++.h>
using namespace std;

int main() {
  int x; cin >> x;
  cout << "FOUND " << x << "\n";
}

It's not hard to compile & run a C++ program. First, open up Powershell on Windows, Terminal on Mac, or your distro's terminal in Linux. We can compile name.cpp into an executable named name with the following command:

g++ name.cpp -o name

Then we can execute the program:

./name

If you type some integer and then press enter, then the program should produce output. We can write both of these commands in a single line:

g++ name.cpp -o name && ./name

Redirecting Input & Output

If you want to read input from inp.txt and write to out.txt, then use the following:

./name < inp.txt > out.txt

See "Intro - Introductory Problems" for how to do file input and output within the program.

Compiler Options (aka Flags)

Use compiler flags to change the way GCC compiles your code. Usually, we use the following in place of g++ name.cpp -o name:

g++ -std=c++17 -O2 name.cpp -o name -Wall -Wextra -Wshadow

Explanation:

  • -O2 tells g++ to compile your code to run more quickly (see here)
  • -std=c++17 allows you to use features that were added to C++ in 2017. USACO currently uses -std=c++11.
  • -Wall -Wextra -Wshadow checks your program for common errors. See "General - Debugging" for more information.
  • Summary of Options

You should always compile with these flags.

Adding Shortcuts (Mac and Linux only)

(alternatives for Windows?)

Of course, retyping the flags above can get tedious. You should define shortcuts so you don't need to type them every time!

Aliases in Terminal

Open your bash profile with a text editor such as gedit (or sublime text).

brew install gedit
gedit ~/.zshenv

You can add aliases and functions here, such as the following to compile and run C++.

co() { g++ -std=c++17 -O2 -o $1 $1.cpp -Wall -Wextra -Wshadow; }
run() { co $1 && ./$1 & fg; }

Now you can easily compile and run name.cpp from the command line with co name && ./name or run name. Note that all occurrences of $1 in the function are replaced with name.