2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++でAtCoderをする最低限環境構築の仕方

Last updated at Posted at 2020-05-17

前振り

AtCoderを始めて、ずっとコードテストを使っていたのですが、AtCoder Beginner Contest 165の解説動画を見て、「え、C++ってWindowsじゃなくても実行できるの?!」と驚き、最低限の環境を作ってみました。

前提

# include <bits/stdc++.h>

を使うのでgcc系のコンパイラを入れます。

  • PCはMacです。
  • テキストエディタとしてATOMを使用します。
  • 最低限の環境なので使いやすさなどは考慮しておらず、macでAtCoderのC++コードを実現することを目標としています。

gccを入れる

Macのターミナルを開いて、homebrew(パッケージ管理システム)でコンパイラであるgccをインストールします。

terminal
$ brew install gcc

pathを変更する

gccを入れた状態では、

  • /usr/bin/g++ (clang)
  • /usr/local/bin/g++-9 (gcc)

の二つのコンパイラが存在していて、この状態でg++コマンドを打ってもclangが呼び出される。
/usr/local/bin/にg++(gcc)のシンボリックリンクを作成し、g++コマンでgccを実行するようにする。

terminal
$ ln -s /usr/local/bin/g++-9 /usr/local/bin/g++

stdc++.hを使えるようにする

gccをインストールすると、/usr/local/下の奥にstdc++.hがあり、コンパイラが認識出来る場所に配置する必要がある。
そこで、/usr/local/下にフォルダを作成し、stdc++.hファイルをコピーして配置する。

terminal
$ cd /usr/local/
$ mkdir /usr/local/include/bits
$ cp ./Cellar/gcc/9.2.0/include/c++/9.2.0/x86_64-apple-darwin18/bits/stdc++.h /usr/local/include/bits/

実行手順

1. ファイル作成

ファイル保存場所に移動し、空ファイルを作成する。

$ cd Desktop/
$ atom hello.cpp

2. プログラム記述

今回は簡単に以下のプログラムを記述しました。

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

int main(void)
{
     cout << "hello world" << endl;
    return 0;
}

3. コンパイル・実行

ファイル名を指定しなければ a.out というファイルがコンパイルによって作成される。
a.out を実行する。

terminal
$ g++ hello.cpp
$ ./a.out
hell world

ファイル名を指定したい場合は以下のようにコンパイルすればよい。

terminal
$ g++ hello.cpp -o world.out

参考

C/C++ 開発環境のインストール
Visual studio codeで競プロ環境構築[Mac OS]

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?