3
3

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 1 year has passed since last update.

Macでgccの環境構築しようと思ったら上手くいかなかった件

Posted at

はじめに

競技プログラミングでは、Macにデフォルトで入っているClangよりgccが良いと聞いたので、gccをインストールしようとしました。
しかし、環境構築でかなり詰まったので、対処のために行なったことを記事として残しておこうと思います。色々試しているため、正直間違ったことをしている部分もあるかもしれませんが、もし困った方がいれば、ご活用ください。

環境

今回は、シリコン製macbookかつhomebrewはインストールしてある前提で話を進めます。そこはご了承ください。

gccのインストール

以下のコマンドをターミナルに打ち込みます。

brew install gcc

gccが入ったかを確認するために以下のコマンドを実行します。

gcc -v

しかし、結果は

Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

とclangが呼び出されてしまいました。なので、gccに正確にパスを通したいと思います。

対処で行ったこと

1.Homebrewパスの優先順位の確認

まずgccの実行元を調べるために、下のコマンドを打ちます。

which gcc

すると

/usr/bin/gcc

と出てきました。
ですが、本来Homebrewにインストールしているため、

/opt/homebrew/bin/gcc

以下のように出てきて欲しい。。。

なので以下のコマンドでHomebrewのパスの優先順位が高くなっているかを確認するため、環境変数の優先順位を確認しましょう。

echo $PATH

自分の場合は以下のような結果が出ました。結果を見て/opt/homebrew/bin/usr/local/binよりも上に来ているか確認しましょう。

/Users/maoz/.pyenv/shims:/Users/maoz/.pyenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands

今回は/opt/homebrew/binが3つ目、/usr/local/binがそれ以降だったので大丈夫でした。(でもここでwhich gccを実行しましたが、clangのままで上手くいきませんでした。)

2.シンボリックリンクの作成

次にシンボリックリンクを作成します。

シンボリックリンクとは?
フォルダのショートカットのようなもので、ファイルをwebページのURLリンクのようにすることができます。

ln コマンドを使って以下のコマンドを打ち込みます。

sudo ln -s /opt/homebrew/bin/gcc-14 /opt/homebrew/bin/gcc
sudo ln -s /opt/homebrew/bin/g++-14 /opt/homebrew/bin/g++

バージョンは以下のコマンドで調べます。

ls /opt/homebrew/bin/gcc-*

ですがここでもwhich gccを実行しましたが、変化はありませんでした。

3.設定ファイル読み込み

基本的にシリコン製Macのターミナルはzshで動いています

環境変数は変更してませんが、念のため、シェルの設定を保存しました。

source ~/.zshrc

このコマンドを打った後、ターミナルを再起動してwhich gccを打つと

/opt/homebrew/bin/gcc

とやっと変更されました!

一応、gcc- vを打つと

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/14.1.0/bin/../libexec/gcc/aarch64-apple-darwin23/14/lto-wrapper
Target: aarch64-apple-darwin23
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 --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.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin23 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.0 (Homebrew GCC 14.1.0)

としっかり変更されていることが確認できました。

ちょっとした考察

ここまで記事をまとめていて思ったのは、gccをhomebrewでインストールし終わった後にターミナルを再起動してみたらもしかしたらできたからすぐできたかもしれないと思いました。今回、色々試した上で、環境変数の設定はいじっていないので、source ~/.zshrcで設定を保存する必要なかったとも思いますし、その上再起動したら上手くいったので原因はわかりません。
ですが、やりたいことはできたので、ほっとしました。この記事がお役に立てれば幸いです。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?