3

I just updated my Mac Book Pro to El Capitan (10.11.4) and gcc 5.2 broke, so I installed gcc 5.3.0 using homebrew but the new compiler is not linked to /usr/local/bin/gcc. Instead it's linked to /usr/local/bin/gcc-5. Likewise, all the related commands (g++, gcc-ar, gcc-ranlib, ...) have now the '-5' appended whereas the plain gcc family w/o '-5' is still linked to 5.2.

Is there a way to force homebrew to link to plain gcc?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JAC
  • 396
  • 3
  • 9

2 Answers2

1
    #!/usr/bin/perl
    # relink_gcc unlinks the old gcc and links the current version
    #
    # SYNOPSIS:
    #   cd /usr/local/bin
    #   relink_gcc
    #
    # DESCRIPTION:
    #   Homebrew installs gcc version N.X as gcc-N. All other components
    #   of the Gnu Compiler Collection are installed with the '-N' suffix.
    #   E.g. g++-N, c++-N, ... However, I prefer to have the names w/o
    #   the '-N' suffix so I can use the same Makefiles on different
    #   computers.  This program changes the links from the plain gcc
    #   (and its family) to point to the most recent gcc version.
    #   Because 'darwin' also changes version, the program changes the
    #   version of programs with 'darwin' in their names to the current
    #   version. The gcc and darwin versions are currently hardcoded.
    #
    # CAVEAT:
    #   Make a backup of /usr/local/bin and /usr/local/Cellar before
    #   using this script
    
    use strict;
    use warnings;
    
    # Set parameters here. I might add command line args at some point.
    #..Dry run only prints what would be done. Does not actually do it.
    my $dryrun = 1;

    my $new_version = 10; 
    my $ending = "-$new_version";
    my $re_ending = qr/$ending/;
    
    # ..In case upgrading one sub-version to another (e.g. 5.2.0 to 5.3.1)
    #   (moot if upgrading to new version (e.g. 5.N.M to 6.K.L)
    my $old_sub = qr/5\.2\.0/;
    my $new_sub = qr/$new_version\.2\.0/;
    # ..To find the Darwin version, at the command line prompt enter
    #       uname -r
    my $new_darwin_version = "17.7.0";
    
    
    
    # No changes needed below this line (I hope)
    
    my @gcc_N_list = glob qq("*${ending}");
    
    print "found $#gcc_N_list files ending in '$ending'\n";
# ..If the file exists but is not a link, leave it alone.
    if (-e $plain && (! -l $plain )){ 
        print "$plain is not a link\n";
        next;
    }   
# ..File pointed to by '$file'
    my $orig = readlink($file);
    if ($dryrun) {print "$file -> $orig\n";}

# __Change versions to current ones if sub-version upgrade
# ..Gnu compiler collection version
    $orig =~ s/${old_sub}/${new_sub}/g;
# ..Apple Darwin version
    $orig =~ s/(darwin)\d{2}\.\d\.\d/$1$new_darwin_version/;

# ..Skip non-existent files
    if (! -e $orig){
        print "\t$orig does not exist. Skip!\n";
        next;
    }
# ..Remove existing files before linking
    if (-e $plain || -l $plain ){
        print "$plain exists\n";
        if ($dryrun) {
            print "\tWould remove $plain\n";
            print "\tunlink $plain\n";
        }
        else {
            unlink $plain or die "Unable to unlink $plain $!\n";
        }
    }
    else {
        print "\t$plain does not exist would create\n";
    }
# ..Finally! link the new version
    if ($dryrun) {
        print "\twould symlink $orig, $plain;\n";
    }
    else {
        symlink $orig, $plain
            or die "Unable to symlink $orig to $plain:$!\n";
    }

}
JAC
  • 396
  • 3
  • 9
  • If you add the perl script you used to your answer I'll up-vote it. :) – l'L'l Apr 09 '16 at 01:44
  • @I'L'I: I uploaded the code. Sorry for the delay (was fixing some other things that broke in the _upgrade_ to El Capitán) – JAC Apr 20 '16 at 15:22
  • No worries, glad you posted the script, hopefully it will help others - cheers! – l'L'l Apr 20 '16 at 19:38
0

Homebrew does not install gcc as gcc, but as gcc-5, to avoid conflicts, so be advised that going and changing things around differently is a potential recipe for mucking things up.

Some possible solutions:

  1. Use the command brew link gcc

    OSX - replace gcc version with version installed via Homebrew

  2. Create symbolic links

    How to symlink a file in Linux?

  3. Read the documentation for tips on editing/modifying defaults

    Homebrew FAQ

The first and last option is likely the more sensible route, but maybe that's not what you're looking for... The documentation will help you understand why they've done it the way they have.

Community
  • 1
  • 1
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • @I'L'I: Regarding Homebrew linking to `gcc-5` instead of plain `gcc`, my problem began because Homebrew linked gcc 5.2 to plain`gcc` but gcc 5.3 to `gcc-5`. Before posting my question I did `brew link gcc`. It linked gcc 5.3 to gcc-5 but left gcc 5.2 linked to gcc (w/o -5). I also read the Homebrew FAQ and the gcc formula, but did not find what to change to prevent Homebrew from appending the '-5'. Regarding symlinks, I considered writing a Perl script to change the links but wanted to see if there is a way to tell Homebrew not to append the '-5'. – JAC Apr 03 '16 at 12:50
  • Are you still using the 5.2 for anything? If not I would uninstall 5.2 via homebrew then relink 5.3, and/or reinstall/update 5.3 afterward. – l'L'l Apr 03 '16 at 16:08
  • @I'L'I: Now that I installed 5.3, I don't need 5.2 anymore. However, re-linking 5.3 links to the '-5' names, so I ended using a short Perl script to link version 5.3 to plain `gcc` (i.e. w/o the '-5' suffix). I found that the problem was that under El Capitán, `gcc` (both 5.2 & 5.3) and `ld` do not search /usr/local/lib and /usr/local/include by default. They have to be explicitly included in the environment variables `LIBRARY_PATH` and `CPLUS_INCLUDE_PATH`, respectively. In addition, `ld` is not the gnu version and thus ignores `LD_LIBRARY_PATH`. – JAC Apr 05 '16 at 14:19
  • @JAC: You should put that as an answer if it solved the issue and then self-accept it (a link to the script might also be helpful) :) – l'L'l Apr 06 '16 at 08:17
  • @I'L'I: Thank you for your suggestion (it shows that I'm new to Stack Overflow, doesn't it?) :( – JAC Apr 07 '16 at 17:32