yna87’s blog

競技プログラミングとかの日記。

Macでgcc, g++をインストールした話(自分用のメモ)

環境

  • Mac Book Air (13-inch, Early 2015)
  • macOS Catalina version10.15.3
  • zsh 5.7.1 
gcc, g++をインストールする

 まず、Homebrewを利用してgcc, g++をインストールした。yna87.hatenablog.com

$ brew search gcc
==> Formulae
gcc                 gcc@5               gcc@7               x86_64-elf-gcc
gcc@4.9             gcc@6               gcc@8
==> Casks
homebrew/cask/gcc-arm-embedded
$ brew install gcc

これでgcc, g++のインストールが完了。しかし、今の状態ではgcc, g++コマンドを使用してもインストールしたgcc, g++が呼び出されないので、次のステップが必要。

$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
インストールしたgcc, g++が呼び出されるようにする 

 インストールしたパッケージは/usr/local/Celler/に保存され、/usr/local/bin/にコマンドのエイリアスが貼られる。まず、エイリアスを探す。

$ ls /usr/local/bin | grep gcc
gcc-9
gcc-ar-9
gcc-nm-9
gcc-ranlib-9
x86_64-apple-darwin19-gcc-9
x86_64-apple-darwin19-gcc-9.2.0
x86_64-apple-darwin19-gcc-ar-9
x86_64-apple-darwin19-gcc-nm-9
x86_64-apple-darwin19-gcc-ranlib-9
$ ls /usr/local/bin | grep g++
g++-9
x86_64-apple-darwin19-g++-9

/usr/local/bin/gcc, /usr/local/bin/g++に対してシンボリックリンクを作成した。

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

 最後に、~/.zshenvに以下の記述を。

export PATH="/usr/local/bin:$PATH"

.zshenvの内容を再読み込みすると、インストールしたgcc, g++がzshで使えるようになりました。

$ source ~/.zshenv
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/9.2.0_3/libexec/gcc/x86_64-apple-darwin19/9.2.0/lto-wrapper
Target: x86_64-apple-darwin19
Configured with: ../configure --build=x86_64-apple-darwin19 --prefix=/usr/local/Cellar/gcc/9.2.0_3 --libdir=/usr/local/Cellar/gcc/9.2.0_3/lib/gcc/9 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 9.2.0_3' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
Thread model: posix
gcc version 9.2.0 (Homebrew GCC 9.2.0_3) 
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/9.2.0_3/libexec/gcc/x86_64-apple-darwin19/9.2.0/lto-wrapper
Target: x86_64-apple-darwin19
Configured with: ../configure --build=x86_64-apple-darwin19 --prefix=/usr/local/Cellar/gcc/9.2.0_3 --libdir=/usr/local/Cellar/gcc/9.2.0_3/lib/gcc/9 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 9.2.0_3' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
Thread model: posix
gcc version 9.2.0 (Homebrew GCC 9.2.0_3)