1. 程式人生 > >mac如何升級g++的版本?更改預設gcc版本

mac如何升級g++的版本?更改預設gcc版本

本文主要參考知乎的答案(https://zhihu.com/question/35215099/answer/63559664)

1. 安裝port: 在port官網中選擇一個適合自己系統的版本下載

2. 安裝後,進入port目錄,我沒有修改預設安裝路徑,port所在位置為:

/opt/local/bin

3. 查詢gcc庫:

./port search gcc #查詢gcc庫
我的顯示如下,顯示的其實挺多的,上下翻看一下所需的版本:

4. 安裝gcc4.8

sudo -i #獲取管理員許可權
#鍵入密碼
./port install gcc49
5. 檢視安裝的版本
./port select --list gcc


6. 切換版本
sudo ./port select --set gcc mp-gcc49


7. 清空bash快取
hash -r

在新的命令列視窗中檢視gcc/g++版本:
UserName:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/4.9.4/lto-wrapper
Target: x86_64-apple-darwin16
Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc49/gcc49/work/gcc-4.9.4/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc49 --includedir=/opt/local/include/gcc49 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.9 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.9 --with-gxx-include-dir=/opt/local/include/gcc49/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --disable-isl-version-check --with-cloog=/opt/local --disable-cloog-version-check --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 --with-pkgversion='MacPorts gcc49 4.9.4_1' --with-build-config=bootstrap-debug
Thread model: posix
gcc version 4.9.4 (MacPorts gcc49 4.9.4_1) 

如果本文對您有用,點個讚唄大笑