Installing GCC on Mac

来源:互联网 发布:sql sqlserver一样吗 编辑:程序博客网 时间:2024/06/05 19:21

Suppose you want to use the latest and greatest version of GCC on the Mac because you want to use features inC++11 that are not yet available in XCode. There are probably a few ways to do this, but one way is viaMacPorts which provides precompiled binaries for tons of packages.

You can follow the following steps to install GCC 4.7 via MacPorts. Others versions (including GCC 4.8) follow a pretty similar procedure.

  1. Browse to the MacPorts website. Download the DMG image for your operating system
  2. Install the package
  3. Open up a terminal Applications > Utilities > Terminal
  4. Make sure that you have the latest version by running the self update via the terminal command
sudo port selfupdate
  1. Install GCC via the terminal command
sudo port install gcc47

Now sit back and wait while MacPorts does its thing. One MacPorts is done, the next thing is to compile a simple program. Running gcc or g++ at the command line invokes the compiler installed with XCode. The new executables are installed to

/opt/local/bin/gcc-mp-4.7

and

/opt/local/bin/g++-mp-4.7
  1. Depending on your build environment, you may want to access GCC by running gcc or g++ at the command line. Because the MacPorts are installed to another location, you either need to either specify the full path, or set the compiler to use on your system. For the second option, you can use MacPorts to select the default compiler. First, identify which versions are installed on your system.
port select --list gcc

In my case, this returns

Available versions for gcc:   gcc42   llvm-gcc42   mp-gcc47   none (active)

To select GCC 4.7, run the command

sudo port select --set gcc mp-gcc47Password:Selecting 'mp-gcc47' for 'gcc' succeeded. 'mp-gcc47' is now active.


在MAC下切换GCC编译器的办法(MacPorts)

这里的办法是通过port命令选项实现的,所以需要先安装MacPorts,具体的安装步骤就不多说了,切换不同版本gcc的命令如下:
执行:
$ sudo port select --list gcc
显示:
Password:
Available versions for gcc:
        gcc42
        llvm-gcc42
        mp-gcc45 (active)
        none
选择llvm-gcc42作为编译器,执行:
$ sudo port select --set gcc llvm-gcc42
显示:
Selecting 'llvm-gcc42' for 'gcc' succeeded. 'llvm-gcc42' is now active.

以前版本的MacPorts是通过 gcc_select 来切换gcc版本的,不过今天我执行无效,一查才知道官网投票取消了 gcc_select,用参数 select 来实现了。

0 0
原创粉丝点击