📓
【macOS】競技プログラミング C++ 環境構築
Homebrew インストール
Homebrew のインストールに関しては、公式サイトを参考にして下さい。
GCC インストール
Homebrew から GCC をインストールします。
brew install gcc
Clang から GCC へ変更
インストールしただけだと Mac に標準搭載されている Clang というコンパイラが使用されます。
そのためにシンボリックリンクを作成し、GCC を使用されるように設定します。
sudo ln -s /opt/homebrew/bin/gcc-{version} /usr/local/bin/gcc
sudo ln -s /opt/homebrew/bin/g++-{version} /usr/local/bin/g++
{version} の中には、インストールした際のバージョンになります。
設定ができたか確認をしたい場合は、which
コマンド か -v
オプション付けてコマンドを叩きます。
which gcc
gcc -v
# 反映前
/usr/bin/gcc
Apple clang version 17.0.0 (clang-1700.0.13.3)
Target: arm64-apple-darwin24.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
# 反映後
/usr/local/bin/gcc
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/14.2.0_1/bin/../libexec/gcc/aarch64-apple-darwin24/14/lto-wrapper
Target: aarch64-apple-darwin24
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,m2 --program-suffix=-14 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 14.2.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin24 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (Homebrew GCC 14.2.0_1)
which g++
g++ -v
# 反映前
/usr/bin/g++
Apple clang version 17.0.0 (clang-1700.0.13.3)
Target: arm64-apple-darwin24.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
# 反映後
/usr/local/bin/g++
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/14.2.0_1/bin/../libexec/gcc/aarch64-apple-darwin24/14/lto-wrapper
Target: aarch64-apple-darwin24
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,m2 --program-suffix=-14 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 14.2.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin24 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (Homebrew GCC 14.2.0_1)
作成したシンボリックリンクを削除したい場合は、unlink
コマンドを叩きます。
sudo unlink /usr/local/bin/gcc
sudo unlink /usr/local/bin/g++
シンボリックリンクを作成した時に、設定が反映されない時があります。
その時は、下記を試してみて下さい。
- ターミナルを再起動
-
source
コマンドを叩く
source ~/.bashrc
もしくは
source ~/.zshrc
VSCode 拡張機能
エディタには VSCode を使用します。
VSCode の拡張機能として下記のものを入れます。
- C/C++
- Code Runner(Cファイルを実行するために必要だが、別の方法もある)
IntelliSense の設定
-
Ctrl+Shift+P
コマンドパレットを開く - C/Cpp: Edit Configurations (JSON) をコマンドパレットに入力し選択する
- c_cpp_properties.json ファイルが作成される
- json ファイルを下記のように設定します。
※ バージョンは、違う可能性があるため注意して下さい。
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/local/bin/g++",
"cStandard": "c23",
"cppStandard": "c++23",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
実行
Cファイルを適当に作成します。
該当ファイルを開き、control + option + N で実行されます。
さいごに
gcc のバージョンが上がるとシンボリックリンクが動作しなくなることがあるため、その際は再度シンボリックリンクを作成するようにしてください。
Discussion