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/Intro_RunningCpp.md
2020-06-09 14:08:30 -04:00

4.8 KiB

slug title author order
/intro/running-cpp Using C++ Nathan Wang, Benjamin Qi 4

Using C++ both online and locally (currently for Mac only).

Running Online

  • CSAcademy
    • pretty nice (unless you get "Estimated Queue Time: ...")
  • Ideone (used this for a while ...)
    • okay with an ad blocker
    • make sure your code is not public
    • sometimes randomly erases your code when you first create it (so get in the habit of copying your code before creating it!)
  • OnlineGDB

Of course, you can't use File I/O on these websites (or do a lot of other stuff ...).

Using C++ Locally (on Mac)

Clang is the default compiler for Mac OS X, but you should use G++.

Installation

Open Terminal and run

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.

Confirmation

You should be able to compile with g++ or maybe g++-#, where # is the version number (currently 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.

Troubleshooting

Make sure you have installed XCode command line tools.

xcode-select --install # make sure x-code command line tools are installed
softwareupdate --list
softwareupdate -i -a # installs everything

Compiling & Running C++ via Terminal

(simple tutorial or video?)

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 we compile name.cpp into an executable named name.

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 "Introductory Problems" for how to do file input and output within the program.

Adding Shortcuts

Retyping this command gets tedious once we start adding many command line options. See "General - Debugging" for more information about compilation options.

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++11 -O2 -o $1 $1.cpp -Wall -Wextra -Wshadow -DLOCAL -Wl,-stack_size -Wl,0xF0000000; }
run() { co $1 && ./$1 & fg; }

Now you can easily compile and run name.cpp from the command line with the following:

run name

Note that all occurrences of $1 are replaced with name.

Tools

IDEs

Text Editors

I mostly just use sublime text.