[c++] Update GCC on OSX

So I am a new programmer and I just installed XCode on my Macbook to get the GCC. I think Xcode is the only way for getting GCC on OSX. Now when I run my Hello World application, in C++, g++ comes up saying it is version 4.0.1 but when I look for commands starting with g I also see g++-4.2. Is there any way of making 4.2 default rather than 4.0.1, and also is there a way to updating gcc to the latest version 4.4.0?

EDIT: Ok, so I installed macports and installed gcc4.4 and it shows up on terminal as gcc-mp-4.4 and how do I make it default with gcc_select, like what are the commands and stuff. Thanks.

This question is related to c++ macos gcc compiler-construction g++

The answer is


You can have multiple versions of GCC on your box, to select the one you want to use call it with full path, e.g. instead of g++ use full path /usr/bin/g++ on command line (depends where your gcc lives).

For compiling projects it depends what system do you use, I'm not sure about Xcode (I'm happy with default atm) but when you use Makefiles you can set GXX=/usr/bin/g++ and so on.

EDIT

There's now a xcrun script that can be queried to select appropriate version of build tools on mac. Apart from man xcrun I've googled this explanation about xcode and command line tools which pretty much summarizes how to use it.


in /usr/bin type

sudo ln -s -f g++-4.2 g++

sudo ln -s -f gcc-4.2 gcc

That should do it.


Whatever Apple ships as the default gcc in xcode (4.2.1 on 10.6, 4.0.1 before) is well tested (and maintained) by the apple guys and the "standard" to build software with on OS X. Everything else is not, so think twice if you want to develop software, or be gcc/OS X beta tester.


You can install your GCC manually

either through

sudo port install gcc46

or your download the source code from one of the mirrors from here for example here

tar xzvf gcc-4.6.0.tar.gz cd gcc-4.6.0 ./configure make

well if you have multiple version, then through you can choose one

port select --list gcc

remember port on mac is called macport https://www.macports.org/install.php and add add the bin into your path export PATH=$PATH:/opt/local/bin


I know it is an old request. But it might still be useful to some. With current versions of MacPorts, you can choose the default gcc version using the port command. To list the available versions of gcc, use:

$ sudo port select --list gcc

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

To set gcc to the MacPorts version:

$ sudo port select --set gcc mp-gcc46


The following recipe using Homebrew worked for me to update to gcc/g++ 4.7:

$ brew tap SynthiNet/synthinet
$ brew install gcc47

Found it on a post here.


I'm just dropping in to say that using a soft link to accomplish this is a terrible, no-good, horrible idea.

One of the key things about writing software is reproduceability - you want to be able to get the same results every time. These systems are so complex that you want to reduce all invisible sources of error.

Having a soft link is an invisible source of error. It's the sort of thing you'll forget in a month, then move to a different machine, and wonder why you are getting different results - or, you'll try to upgrade your system, and you'll get weird errors because it's not expecting a softlink there.

Moreover, this isn't guaranteed to work - in particular, it's not clear that you will get the correct system include files, which have certainly changed between iterations of gcc.

gcc_select is a systematic way of doing the same thing which will work predictably, or in the very worst case you can file a bug report and get an eventual fix or fix it yourself.

Unfortunately :-( gcc_select does not affect which compiler XCode uses so it's not the way to go if you need to work in XCode (which I do). I still don't know what that way might be.


use "gcc_select -l"

> gcc_select -l

gcc40 mp-gcc44

> gcc_select mp-gcc44


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to compiler-construction

fatal error C1010 - "stdafx.h" in Visual Studio how can this be corrected? Compilation error: stray ‘\302’ in program etc What is difference between sjlj vs dwarf vs seh? What is the difference between a token and a lexeme? How to compile makefile using MinGW? C++ variable has initializer but incomplete type? It is more efficient to use if-return-return or if-else-return? Could not load file or assembly ... The parameter is incorrect How do I compile the asm generated by GCC? Visual Studio: LINK : fatal error LNK1181: cannot open input file

Examples related to g++

How do I set up CLion to compile and run? Compile c++14-code with g++ Fatal error: iostream: No such file or directory in compiling C program using GCC How does #include <bits/stdc++.h> work in C++? DSO missing from command line C++ unordered_map using a custom class type as the key How do I enable C++11 in gcc? usr/bin/ld: cannot find -l<nameOfTheLibrary> cc1plus: error: unrecognized command line option "-std=c++11" with g++ to_string is not a member of std, says g++ (mingw)