OSX - replace gcc version 4.2.1 with 4.9 installed

2019-01-07 06:18发布

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 GCC version on my mac It returns the following

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

I have also installed the most recent GCC version using Homebrew with

brew install gcc49

My question now is how do I apply that newly installed GCC version to be the default version that the terminal uses?

I am also aware that when you use homebrew to isntall gcc it names it gcc-49 so that there is no confusion between packages.

I have no idea how to replace the 4.2.1 version that comes with XCode with the 4.9 version I have installed.

Thanks

Edit: Switched to my mac to get the full return statement of gcc --version

Edit 2: My end game here is to be able to navigate to the directory and be able to type

make
sudo make install

to install the daemon that has been made. Right now that returns tons of errors with random packages and the Standard Library

4条回答
【Aperson】
2楼-- · 2019-01-07 06:25

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
查看更多
冷血范
3楼-- · 2019-01-07 06:35

OS X does not come with GCC installed (4.2.1 or otherwise). Clang is the default system compiler and has been for some time. It is using the C++ headers from 4.2.1 when invoked as GCC. Have you tried compiling your code with Clang natively, instead of calling "gcc" (which calls Clang)? It has more modern headers and C++ support than the GCC emulation mode.

查看更多
欢心
4楼-- · 2019-01-07 06:35

Download the gcc binaries untar and copy the bin, lib include share and libexec files to your /usr directory then type gcc --version this is what i expect you to see

gcc --version gcc (GCC) 4.9.2 20141029 (prerelease) Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

查看更多
Anthone
5楼-- · 2019-01-07 06:38

simply updating the order of $PATH in ~/.bash_profile to the brew installed version 'export PATH=/usr/local/Cellar/gcc/5.1.0/bin:$PATH' was not enough to make the switch for me

changing the alias in your ~./bash_profile (alias gcc='gcc-5') works, but can be confusing i.e. which gcc will return the Clang version

what worked for me was to make a symbolic link in the brew gcc directory as well as update the path (point 1 above)

cd /usr/local/Cellar/gcc/5.1.0/bin/gcc
ln -s gcc-5 gcc

now which gcc returns the correct version 5.1.0

查看更多
登录 后发表回答