【问题标题】:Homebrew does not link gcc 5.3 to /usr/local/bin/gcc (OS X 10.11.4 El Capitan)Homebrew 没有将 gcc 5.3 链接到 /usr/local/bin/gcc (OS X 10.11.4 El Capitan)
【发布时间】:2016-07-21 20:44:34
【问题描述】:

我刚刚将我的 Mac Book Pro 更新到 El Capitan (10.11.4) 并且 gcc 5.2 坏了,所以我使用自制软件安装了 gcc 5.3.0,但新的编译器没有链接到 /usr/local/bin/gcc。相反,它链接到/usr/local/bin/gcc-5。同样,所有相关命令(g++、gcc-ar、gcc-ranlib、...)现在都附加了“-5”,而没有“-5”的普通 gcc 系列仍然链接到 5.2。

有没有办法强制自制程序链接到普通 gcc?

【问题讨论】:

    标签: macos gcc homebrew


    【解决方案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 脚本添加到您的答案中,我会对其进行投票。 :)
    • @I'L'I:我上传了代码。抱歉耽搁了(正在修复升级到El Capitán的其他一些问题)
    • 不用担心,很高兴您发布了脚本,希望它可以帮助其他人 - 干杯!
    【解决方案2】:

    Homebrew 不会将 gcc 安装为 gcc,而是安装为 gcc-5,以避免冲突,因此请注意,以不同的方式进行更改是搞砸事情的潜在秘诀。

    一些可能的解决方案:

    1. 使用命令brew link gcc

      OSX - replace gcc version with version installed via Homebrew

    2. 创建符号链接

      How to symlink a file in Linux?

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

      Homebrew FAQ

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

    【讨论】:

    • @I'L'I:关于 Homebrew 链接到 gcc-5 而不是普通的 gcc,我的问题开始是因为 Homebrew 将 gcc 5.2 链接到 plaingcc 但 gcc 5.3 链接到 gcc-5。在发布我的问题之前,我做了brew link gcc。它将 gcc 5.3 链接到 gcc-5 但将 gcc 5.2 链接到 gcc (w/o -5)。我还阅读了 Homebrew 常见问题解答和 gcc 公式,但没有找到要更改的内容以防止 Homebrew 附加“-5”。关于符号链接,我考虑编写一个 Perl 脚本来更改链接,但想看看是否有办法告诉 Homebrew 不要附加“-5”。
    • 你还在用 5.2 做任何事吗?如果不是,我会通过自制软件卸载 5.2,然后重新链接 5.3,和/或之后重新安装/更新 5.3。
    • @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_PATHCPLUS_INCLUDE_PATH 中。此外,ld 不是 gnu 版本,因此忽略了LD_LIBRARY_PATH
    • @JAC:如果它解决了问题,你应该把它作为答案,然后自我接受它(脚本的链接也可能有帮助):)
    • @I'L'I:谢谢你的建议(这表明我是 Stack Overflow 的新手,不是吗?):(
    猜你喜欢
    • 2014-01-06
    • 2018-04-28
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2013-11-01
    • 2011-05-21
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多