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.mdx

230 lines
8.6 KiB
Text
Raw Normal View History

2020-06-08 20:42:55 +00:00
---
2020-06-15 23:19:07 +00:00
id: running-cpp
title: Running C++
author: Nathan Wang, Benjamin Qi, Anthony Wang
2020-06-22 19:59:16 +00:00
description: Options for running C++ both online and locally.
2020-06-08 20:42:55 +00:00
---
2020-06-23 03:45:59 +00:00
Please let us know if these installation instructions do not work for you.
2020-06-23 01:00:35 +00:00
# Using C++ Online
2020-06-09 00:47:37 +00:00
2020-06-17 18:05:19 +00:00
- [OnlineGDB](https://www.onlinegdb.com/)
- online compiler with an embedded GDB debugger
- can be buggy sometimes
- supports files and file I/O
- [CSAcademy](https://csacademy.com/workspace/)
- 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](http://ideone.com/)
2020-06-24 21:29:24 +00:00
- okay ... has the bare minimum you need for running C++
2020-06-17 18:05:19 +00:00
- 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)
2020-06-22 14:26:06 +00:00
You can share code with [pastebin](https://pastebin.com/) or [hastebin](https://hastebin.com/).
2020-06-23 01:00:35 +00:00
# Using an IDE
2020-06-08 19:51:58 +00:00
2020-06-23 01:00:35 +00:00
These often have C++ support already built-in.
2020-06-22 14:26:06 +00:00
2020-06-29 01:09:52 +00:00
<resources>
<resource title="Visual Studio Code" url="https://code.visualstudio.com/" starred>Lightweight, fast IDE, but requires some configuration. See <a href="http://www.csc.kth.se/~jsannemo/slask/main.pdf">PAPC 2.1</a> for setup instructions.</resource>
<resource title="Geany" url="https://www.geany.org/" starred>Lightweight, frequently used at IOI.</resource>
<resource title="Visual Studio" url="https://visualstudio.microsoft.com/vs/">Heavier cousin of VS Code. VS Code is better for competitive programming.</resource>
<resource title="Codeblocks" url="http://www.codeblocks.org/">Bad on Mac.</resource>
<resource title="XCode" url="https://developer.apple.com/xcode/">Mac only.</resource>
<resource title="CLion" url="https://www.jetbrains.com/clion/">Requires a license, but <a href="https://www.jetbrains.com/community/education/#students">free for students</a>.</resource>
</resources>
2020-06-19 21:31:29 +00:00
2020-06-23 01:00:35 +00:00
# Using Command Line
2020-06-19 21:31:29 +00:00
2020-06-23 01:00:35 +00:00
Alternatively, run C++ from the command line and use a text editor of your choice.
2020-06-19 21:31:29 +00:00
2020-06-23 01:00:35 +00:00
## Text Editors
2020-06-19 21:31:29 +00:00
2020-06-29 01:09:52 +00:00
<resources>
<resource title="Sublime Text 3" url="https://www.sublimetext.com/" starred>Fast, lightweight. Keeps asking you to purchase a license ...</resource>
<resource title="Atom" url="https://atom.io/">From the makers of Github.</resource>
<resource title="Vim" url="https://www.vim.org/">Classic text editor, usually preinstalled on Linux.</resource>
</resources>
Vim is probably the easiest way to print syntax-highlighted code on Mac, see the response to [this post](https://stackoverflow.com/questions/1656914/printing-code-with-syntax-highlighting).
### Sublime Text Notes
- [Editing Build Settings](https://stackoverflow.com/questions/23789410/how-to-edit-sublime-text-build-settings)
- no need to do this if you just use command line to compile & run
- [FastOlympicCoding Addon](https://github.com/Jatana/FastOlympicCoding)
- see "Debugging" for another way to stress test
- [Sublime Snippets](https://www.granneman.com/webdev/editors/sublime-text/top-features-of-sublime-text/quickly-insert-text-and-code-with-sublime-text-snippets)
- Ben - I use to insert templates
- [Symlink](https://www.sublimetext.com/docs/3/osx_command_line.html)
- Ben - Using `/usr/local/bin/subl` instead of `~/bin/subl` worked for me on OS X Mojave.
2020-06-23 01:00:35 +00:00
## 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](http://mingw.org/).
2020-06-19 21:31:29 +00:00
First, download and run the [MinGW installer](https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/). 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](https://www.rose-hulman.edu/class/csse/resources/MinGW/installation.htm)
2020-06-23 01:00:35 +00:00
### 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.
2020-06-19 21:31:29 +00:00
2020-06-23 03:45:59 +00:00
[VSCode Docs for WSL](https://code.visualstudio.com/docs/cpp/config-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.
2020-06-19 21:31:29 +00:00
## On Mac
2020-06-08 19:51:58 +00:00
2020-06-19 01:15:13 +00:00
[Clang](https://en.wikipedia.org/wiki/Clang) is the default compiler for Mac OS X, but you should use [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)'s g++ since that's what [USACO](http://www.usaco.org/index.php?page=instructions) uses to compile your code.
2020-06-09 00:47:37 +00:00
### Installation
2020-06-09 00:47:37 +00:00
2020-06-23 01:00:35 +00:00
1. Open the **Terminal** application and familiarize yourself with some basic commands.
2020-06-09 00:47:37 +00:00
2020-06-23 01:00:35 +00:00
- [Intro to OS X Command Line](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line).
- [Mac Terminal Cheat Sheet](https://www.makeuseof.com/tag/mac-terminal-commands-cheat-sheet/)
2020-06-09 00:47:37 +00:00
2020-06-23 01:00:35 +00:00
2. Install XCode command line tools.
2020-06-08 19:51:58 +00:00
2020-06-23 01:00:35 +00:00
```
xcode-select --install
```
2020-06-08 19:51:58 +00:00
2020-06-23 01:00:35 +00:00
If you previously installed these you may need to update them:
2020-06-08 19:51:58 +00:00
2020-06-23 01:00:35 +00:00
```
softwareupdate --list # list updates
softwareupdate -i -a # installs all updates
```
2020-06-09 17:47:40 +00:00
2020-06-23 01:00:35 +00:00
3. Install [Homebrew](https://brew.sh/).
2020-06-09 17:47:40 +00:00
2020-06-23 01:00:35 +00:00
4. Install gcc with Homebrew.
2020-06-09 17:47:40 +00:00
2020-06-23 01:00:35 +00:00
```
brew install gcc
```
2020-06-08 19:51:58 +00:00
2020-06-23 01:00:35 +00:00
According to [this](https://stackoverflow.com/questions/30998890/installing-opencv-with-brew-never-finishes) if `brew` doesn't seem to finish for a long time then
2020-06-09 18:08:30 +00:00
2020-06-23 01:00:35 +00:00
```
brew install gcc --force-bottle
```
2020-06-09 18:08:30 +00:00
2020-06-23 01:00:35 +00:00
probably suffices.
2020-06-09 18:08:30 +00:00
2020-06-23 01:00:35 +00:00
5. You should be able to compile with `g++` or maybe `g++-#`, where # is the version number (ex. 9). Running the following command
2020-06-11 01:08:14 +00:00
2020-06-23 01:00:35 +00:00
```
g++-9 --version
```
2020-06-11 01:08:14 +00:00
2020-06-23 01:00:35 +00:00
should display something like this:
2020-06-11 01:08:14 +00:00
2020-06-23 01:00:35 +00:00
```
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.
```
2020-06-11 01:08:14 +00:00
2020-06-19 01:15:13 +00:00
## Running With the Command Line
2020-06-09 17:47:40 +00:00
2020-06-09 18:05:18 +00:00
Consider a simple program such as the following, which we'll save in `name.cpp`.
2020-06-09 17:47:40 +00:00
2020-06-09 18:05:18 +00:00
```cpp
#include <bits/stdc++.h>
using namespace std;
2020-06-09 17:47:40 +00:00
2020-06-09 18:05:18 +00:00
int main() {
int x; cin >> x;
cout << "FOUND " << x << "\n";
}
2020-06-09 17:47:40 +00:00
```
2020-06-09 18:05:18 +00:00
2020-06-19 21:31:29 +00:00
It's not hard to [compile & run a C++ program](https://www.tutorialspoint.com/How-to-compile-and-run-the-Cplusplus-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:
2020-06-09 18:05:18 +00:00
2020-06-19 01:36:02 +00:00
```
2020-06-09 17:47:40 +00:00
g++ name.cpp -o name
```
Then we can execute the program:
2020-06-19 01:36:02 +00:00
```
2020-06-09 17:47:40 +00:00
./name
```
2020-06-09 18:08:30 +00:00
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:
2020-06-09 17:47:40 +00:00
2020-06-19 01:36:02 +00:00
```
2020-06-09 17:47:40 +00:00
g++ name.cpp -o name && ./name
```
2020-06-09 18:05:18 +00:00
### Redirecting Input & Output
If you want to read input from `inp.txt` and write to `out.txt`, then use the following:
2020-06-19 01:36:02 +00:00
```
2020-06-09 18:08:30 +00:00
./name < inp.txt > out.txt
2020-06-09 18:05:18 +00:00
```
2020-06-09 18:34:04 +00:00
See "Intro - Introductory Problems" for how to do file input and output within the program.
2020-06-09 17:47:40 +00:00
2020-06-19 01:36:02 +00:00
### Compiler Options (aka Flags)
Use [compiler flags](https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/) 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](https://www.rapidtables.com/code/linux/gcc/gcc-o.html))
2020-06-23 01:00:35 +00:00
- `-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.
2020-06-19 01:36:02 +00:00
- [Summary of Options](https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html)
You should always compile with these flags.
2020-06-11 16:27:28 +00:00
### Adding Shortcuts (Mac and Linux only)
(alternatives for Windows?)
2020-06-08 19:51:58 +00:00
2020-06-19 01:36:02 +00:00
Of course, retyping the flags above can get tedious. You should define shortcuts so you don't need to type them every time!
2020-06-09 18:34:04 +00:00
2020-06-19 01:36:02 +00:00
[Aliases in Terminal](https://jonsuh.com/blog/bash-command-line-shortcuts/)
2020-06-09 17:47:40 +00:00
2020-06-08 19:51:58 +00:00
Open your bash profile with a text editor such as gedit (or sublime text).
2020-06-09 17:47:40 +00:00
2020-06-19 01:36:02 +00:00
```
2020-06-08 19:51:58 +00:00
brew install gedit
gedit ~/.zshenv
```
2020-06-09 17:47:40 +00:00
2020-06-19 01:36:02 +00:00
You can add **aliases** and **functions** here, such as the following to compile and run C++.
2020-06-09 17:47:40 +00:00
2020-06-08 19:51:58 +00:00
```
2020-06-19 01:36:02 +00:00
co() { g++ -std=c++17 -O2 -o $1 $1.cpp -Wall -Wextra -Wshadow; }
run() { co $1 && ./$1 & fg; }
2020-06-08 19:51:58 +00:00
```
2020-06-19 01:36:02 +00:00
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`.