Macのgccを変更してみた.
最近Macに自然言語処理に必要なMeCabをインストールする機会があったのですが,環境構築の段階でgccのエラーを発症.
gcc5を入れることになったので,その手順を忘れないためにメモメモ……
インストール環境
今回はmacの環境を作るのにちょうどいい機会だったので,
パッケージ管理システムであるMacPortsを入れるところから始めます!!
Macportのダウンロード
では,早速下記のサイトで
右上のDownload
もしくは,左側の”Available Downloads
→macのバージョンにあったもの”をダウンロードしてきます.
ダウンロードしてきたものをクリックするとインストーラーが起動するので,だいたい次へ
をクリックでおk.
すると,インストーラーが自動で下記の設定をシェルに書き込んでくれるらしいです
(自分の場合は全く書き込まれてなかったので手動で~/.zshrc
に全部書き込みました)
# MacPorts Installer addition on 2014-09-25_at_18:39:26: adding an appropriate PATH variable for use with MacPorts. export PATH="/opt/local/bin:/opt/local/sbin:$PATH" export MANPATH="/opt/local/man:$MANPATH" # Finished adapting your PATH environment variable for use with MacPorts.
書く場所は必ずしも~/.zshrc
ではなく,~/.bashrc
や~/.profile
等,自分のシェルの設定があるファイルで大丈夫です.
また,書き込んだだけでは設定が反映されないので,source ~/.zshrc
などして設定を反映させてください.
反映したら実際に動くか確認してみます.
$ port version Version: 2.4.2
大丈夫そうですね.これでMacportを入れる作業は終了…!!!!
次はやっと本題のgcc5をインストールしていきます.
gcc5をインストールしてみる
先ほどMacportをインストールしたと思うのですが,念のため一度macport自身のアップデートをしておきます.
sudo port selfupdate # macportは/opt/local以下を編集しているらしいので実行時にsudoをつけないとエラー吐かれることがあります.
次に現在の自分のgccバージョンとmacportでインストール可能なバージョンを見ていきます.
まずは現在の自分のgccバージョンから.
$ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/7.2.0/lto-wrapper Target: x86_64-apple-darwin16.1.0 Configured with: ../configure --build=x86_64-apple-darwin16.1.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-7 --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 --enable-checking=release --with-pkgversion='Homebrew GCC 7.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk Thread model: posix gcc version 7.2.0 (Homebrew GCC 7.2.0)
今のgccのバージョンは以前homebrewでインストールしたもののようです.
では次にmacportでインストール可能なバージョンの一覧を調べます.
$ port search gcc apple-gcc40 @5494 (lang) Apple's version of gcc 4.0 省略 gcc5 @5.5.0 (lang) The GNU compiler collection gcc6 @6.4.0 (lang) The GNU compiler collection 省略
gcc5がありました!!インストール可能です!
なので早速gcc5
をインストールしていきます.
$ sudo port install gcc5
これでgcc5のインストールは完了です!!(簡単!!)
ただ,このままではまだgcc5に設定はされていないので,gcc5に切り替えていきます.
まずmacportで管理されているgccの一覧を取得し,
$ port select --list gcc Available versions for gcc: mp-gcc5 none (active)
この中から使うgccを選択して以下のコマンドで切り替えます.
$ sudo port select --set gcc mp-gcc5 Selecting 'mp-gcc5' for 'gcc' succeeded. 'mp-gcc5' is now active.
この操作を行ったらシェルの方にも反映させる必要があるので,
$ source ~/.zshrc
これでgcc5への切り替えが完了しました!!!
もう一度gccのバージョンを確認してみます.
$ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/5.5.0/lto-wrapper Target: x86_64-apple-darwin16 Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc5/gcc5/work/gcc-5.5.0/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc5 --includedir=/opt/local/include/gcc5 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-5 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-5 --with-gxx-include-dir=/opt/local/include/gcc5/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-tls --with-pkgversion='MacPorts gcc5 5.5.0_0' Thread model: posix gcc version 5.5.0 (MacPorts gcc5 5.5.0_0)
ちゃんとgcc5へと切り替わっていますね!!
………実は上で紹介したバージョンを切り替える方法以外にも,gccという名前に直接リンクする方法もあるのですが,リンクを失敗したりするとあとあと厄介なので私はこちらの方をお勧めしたいです…!!(切り替えも楽だしね!!←)
ただ,gcc5のような新しい名前でリンクを通すというサイトも見かけたのですが,それはそれで切り替えも要らず使い勝手良さげだったので自分の好みに合わせて使い分けてください!!