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

169 lines
4.7 KiB
Markdown
Raw Normal View History

2020-06-08 20:42:55 +00:00
---
slug: /intro/running-cpp
2020-06-09 17:47:40 +00:00
title: Using C++
2020-06-08 20:42:55 +00:00
author: Nathan Wang, Benjamin Qi
order: 4
---
2020-06-09 17:47:40 +00:00
Using C++ both online and locally (currently for Mac only).
2020-06-08 20:42:55 +00:00
<!-- END DESCRIPTION -->
2020-06-09 00:47:37 +00:00
## Running Online
* [CSAcademy](https://csacademy.com/workspace/)
* pretty nice (unless you get "Estimated Queue Time: ...")
* [Ideone](http://ideone.com/) (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](https://www.onlinegdb.com/)
2020-06-09 18:34:04 +00:00
* compiler and debugger tool
2020-06-08 20:42:55 +00:00
2020-06-09 00:47:37 +00:00
Of course, you can't use File I/O on these websites (or do a lot of other stuff ...).
2020-06-08 19:51:58 +00:00
2020-06-09 18:08:30 +00:00
# Using C++ Locally (on Mac)
2020-06-08 19:51:58 +00:00
2020-06-09 00:47:37 +00:00
[Clang](https://en.wikipedia.org/wiki/Clang) is the default compiler for Mac OS X, but you should use [G++](https://en.wikipedia.org/wiki/GNU_Compiler_Collection).
2020-06-09 18:08:30 +00:00
## Installation
2020-06-09 00:47:37 +00:00
2020-06-09 18:34:04 +00:00
Open **Terminal**. First, familiarize yourself with some basic commands given [here](https://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line).
Run
2020-06-08 19:51:58 +00:00
2020-06-09 18:05:18 +00:00
```sh
2020-06-08 19:51:58 +00:00
brew install gcc
```
2020-06-09 00:47:37 +00:00
2020-06-08 19:51:58 +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 00:47:37 +00:00
2020-06-09 18:05:18 +00:00
```sh
2020-06-08 19:51:58 +00:00
brew install gcc --force-bottle
```
2020-06-09 00:47:37 +00:00
probably suffices.
2020-06-08 19:51:58 +00:00
### Confirmation
You should be able to compile with g++ or maybe g++-#, where # is the version number (currently 9). Running the following command:
2020-06-09 17:47:40 +00:00
2020-06-09 18:05:18 +00:00
```sh
2020-06-08 19:51:58 +00:00
g++-9 --version
```
2020-06-09 17:47:40 +00:00
2020-06-08 19:51:58 +00:00
should display something like this:
2020-06-09 17:47:40 +00:00
2020-06-08 19:51:58 +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-09 18:08:30 +00:00
### Troubleshooting
Make sure you have installed XCode command line tools.
```sh
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?)
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
It's not hard to [compile & run a C++ program](https://www.tutorialspoint.com/How-to-compile-and-run-the-Cplusplus-program). First we compile `name.cpp` into an executable named `name`.
```sh
2020-06-09 17:47:40 +00:00
g++ name.cpp -o name
```
Then we can execute the program:
2020-06-09 18:05:18 +00:00
```sh
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-09 18:05:18 +00:00
```sh
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:
```sh
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-09 00:47:37 +00:00
### Adding Shortcuts
2020-06-08 19:51:58 +00:00
2020-06-09 18:34:04 +00:00
[Aliases in Terminal](https://jonsuh.com/blog/bash-command-line-shortcuts/)
Retyping the commands for compiling and running gets tedious once we start adding many [command line options](https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/). See "General - Debugging" for more information about compilation options.
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-09 18:05:18 +00:00
```sh
2020-06-08 19:51:58 +00:00
brew install gedit
gedit ~/.zshenv
```
2020-06-09 17:47:40 +00:00
You can add aliases and functions here, such as the following to compile and run C++.
2020-06-09 18:05:18 +00:00
```sh
2020-06-09 17:47:40 +00:00
co() { g++ -std=c++11 -O2 -o $1 $1.cpp -Wall -Wextra -Wshadow -DLOCAL -Wl,-stack_size -Wl,0xF0000000; }
run() { co $1 && ./$1 & fg; }
2020-06-08 19:51:58 +00:00
```
2020-06-09 17:47:40 +00:00
Now you can easily compile and run `name.cpp` from the command line with the following:
2020-06-09 18:05:18 +00:00
```sh
2020-06-09 17:47:40 +00:00
run name
2020-06-08 19:51:58 +00:00
```
2020-06-09 17:47:40 +00:00
Note that all occurrences of `$1` are replaced with `name`.
2020-06-08 19:51:58 +00:00
## Tools
2020-06-09 00:47:37 +00:00
### IDEs
2020-06-08 19:51:58 +00:00
* [Geany](https://www.geany.org/)
2020-06-09 17:47:40 +00:00
* used at IOI
2020-06-08 19:51:58 +00:00
* [Visual Studio Code](https://code.visualstudio.com/)
2020-06-09 18:34:04 +00:00
* haven't used personally
2020-06-08 19:51:58 +00:00
* [XCode](https://developer.apple.com/xcode/)
2020-06-09 00:47:37 +00:00
* Mac only
2020-06-08 19:51:58 +00:00
* [Codeblocks](http://www.codeblocks.org/)
2020-06-09 00:47:37 +00:00
* bad on Mac :(
2020-06-08 19:51:58 +00:00
### Text Editors
2020-06-09 18:08:30 +00:00
I mostly just use sublime text.
2020-06-08 19:51:58 +00:00
* [Sublime Text 3](https://www.sublimetext.com/)
2020-06-09 00:47:37 +00:00
* [Editing Build Settings](https://stackoverflow.com/questions/23789410/how-to-edit-sublime-text-build-settings)
* [FastOlympicCoding Addon](https://github.com/Jatana/FastOlympicCoding)
* [Sublime Snippets](https://www.granneman.com/webdev/editors/sublime-text/top-features-of-sublime-text/quickly-insert-text-and-code-with-sublime-text-snippets)
* [Symlink](https://www.sublimetext.com/docs/3/osx_command_line.html)
* Using `/usr/local/bin/subl` instead of `~/bin/subl` worked for me on OS X Mojave.
2020-06-09 18:34:04 +00:00
* [Atom](https://atom.io/)