Homebrew无法将gcc 5.3链接到/usr/local/bin/gcc(OS X 10.11.4 El Capitan)

3
我刚把我的Mac Book Pro升级到El Capitan(10.11.4),gcc 5.2损坏了,所以我使用Homebrew安装了gcc 5.3.0,但新的编译器没有链接到/usr/local/bin/gcc。相反,它链接到/usr/local/bin/gcc-5。同样地,所有相关命令(g++,gcc-ar,gcc-ranlib等)现在都有'-5'附加在后面,而没有'-5'的普通gcc系列仍然链接到5.2。
是否有一种方法强制Homebrew链接到普通的gcc?
2个回答

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";
    }

}

如果您将使用的Perl脚本添加到答案中,我会给它点赞。 :) - l'L'l
@I'L'I:我上传了代码。很抱歉耽误了(因为在更新到El Capitán时修复了一些其他问题)。 - JAC
没问题,很高兴你发布了这个脚本,希望它能帮到其他人 - 干杯! - l'L'l

0

Homebrew不会将gcc安装为gcc,而是作为gcc-5安装,以避免冲突,因此请注意,随意更改可能会导致混乱。

一些可能的解决方案:

  1. 使用命令brew link gcc

    OSX - 用Homebrew安装的版本替换gcc版本

  2. 创建符号链接

    如何在Linux中创建符号链接?

  3. 阅读文档以获取有关编辑/修改默认值的提示

    Homebrew FAQ

第一个和最后一个选项可能是更明智的选择,但也许这不是你想要的...文档将帮助您了解他们为什么这样做。


关于Homebrew链接到gcc-5而不是普通的gcc,我的问题是因为Homebrew将gcc 5.2链接到普通的gcc,但将gcc 5.3链接到gcc-5。在发布我的问题之前,我执行了brew link gcc。它将gcc 5.3链接到gcc-5,但将gcc 5.2链接到gcc(没有-5)。我还阅读了Homebrew FAQ和gcc公式,但没有找到要更改的内容,以防止Homebrew添加“-5”。关于符号链接,我考虑编写一个Perl脚本来更改链接,但想看看是否有办法告诉Homebrew不要添加“-5”。 - JAC
你还在使用5.2吗?如果不是的话,我建议你通过Homebrew卸载5.2,然后重新链接5.3,或者在此之后重新安装/更新5.3。 - l'L'l
@I'L'I:现在我安装了5.3,不再需要5.2。然而,重新链接5.3链接到“-5”名称,因此我最终使用一个简短的Perl脚本将版本5.3链接到普通的gcc(即没有“-5”后缀)。我发现问题是,在El Capitán下,“gcc”(5.2和5.3)和“ld”默认不会搜索/usr/local/lib和/usr/local/include。它们必须明确地包含在环境变量LIBRARY_PATH和CPLUS_INCLUDE_PATH中。此外,“ld”不是gnu版本,因此忽略LD_LIBRARY_PATH。 - JAC
@JAC:如果这解决了问题,你应该将其作为答案并自我接受(附上脚本链接也可能会有帮助):) - l'L'l
@I'L'I:感谢您的建议(这表明我是 Stack Overflow 的新手,不是吗?):( - JAC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接