本文主要参考知乎的答案(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
./port select --list gcc
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)
如果本文对您有用,点个赞呗