OSX - replace gcc version 4.2.1 with 4.9 installed via Homebrew

后端 未结 4 1295
你的背包
你的背包 2020-11-28 20:00

This has been plaguing me for awhile now. I am trying to compile a huge C++ file (I know it works as I it works fine on my Arch Linux computer at work). When I checked my GC

4条回答
  •  误落风尘
    2020-11-28 20:50

    By default, homebrew places the executables (binaries) for the packages it installs into /usr/local/bin - which is a pretty sensible place for binaries installed by local users when you think about it - compared to /bin which houses standardisded binaries belonging to the core OS. So, your brew command should have installed gcc-4.9 into /usr/local/bin. The question is now how to use it... you have several options.

    Option 1

    If you just want to compile one or two things today and tomorrow, and then probably not use the compiler again, you may as well just invoke the gcc installed by homebrew with the full path like this:

    /usr/local/bin/gcc-4.9 --version
    

    Option 2

    If you are going to be using gcc quite a lot, it gets a bit tiresome explicitly typing the full path every time, so you could put the following into your ~/.bash_profile

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

    and then start a new Terminal and it will know it needs to look in /usr/local/bin, so you will be able to get away with simply typing

    gcc-4.9 --version
    

    Option 3

    If you just want to use gcc to invoke the compiler, without worrying about the actual version, you can do Option 2 above and additionally create a symbolic link like this

    cd /usr/local/bin
    ln -s  gcc-4.9  gcc
    

    That will allow you to run the homebrew-installed gcc by simply typing gcc at the command line, like this

    gcc --version
    

    Note:

    If you later want to install, say gcc-4.13 or somesuch, you would do your brew install as before, then change the symbolic link like this:

    cd /usr/local/bin
    rm gcc               # remove old link from gcc to gcc-4.9
    ln -s gcc-4.13 gcc   # make new link from gcc to gcc-4.13
    

    Note that if you are actually using C++ rather than C, you will need to adapt the above for g++ in place of gcc.

提交回复
热议问题