如何强制已安装的 gcc-ar 使用特定的 binutils 版本?

How to enforce already installed gcc-ar to use specific binutils version?

我已经从源代码构建了 GCC 4.9.3,并使用一些前缀安装到我的主目录中,例如gcc4.9.

现在我想使用更新版本的 binutils 和 GCC 4.9.3。我已经构建它们并单独安装在我的主目录中,前缀为 binutils2.26.

如何强制 gcc4.9 中的 gcc-ar 使用 binutils2.26 中的 ar 而不是系统一?它总是调用 /usr/bin/ar 并且看起来没有可指定的选项。以某种方式替换 /usr/bin/ar 不是一个选项 - 我在这台机器上没有 root 访问权限。

使用 GCC 的 -B 标志并将其指向包含您要执行的 ar 的目录。有关此标志的更多详细信息,请参阅 GCC manual

gcc-ar -B/path/to/your/dir ...

它似乎对我有用:

$ strace -f -eexecve gcc-ar rc foo.a /dev/null |& grep /ar
[pid 14485] execve("/usr/lib/gcc/x86_64-pc-linux-gnu/5.3.0/../../../../x86_64-pc-linux-gnu/bin/ar", [...]) = 0
$ strace -f -eexecve gcc-ar rc foo.a /dev/null -B/usr/bin |& grep /ar
[pid 14493] execve("/usr/bin/ar", [...]) = 0
$ strace -f -eexecve gcc-ar rc foo.a /dev/null -B/usr/x86_64-pc-linux-gnu/binutils-bin/2.26/ |& grep /ar
[pid 14500] execve("/usr/x86_64-pc-linux-gnu/binutils-bin/2.26/ar", [...]) = 0

我设法解决了这个问题。

使用 strace,我发现 gcc-ar 在几个目录中查找 ar,包括 <gcc install dir>/libexec/gcc/x86_64-redhat-linux/4.9.3.

所以显而易见的解决方案是在此目录中创建指向相应 binutils2.26 可执行文件的链接:

  cd "<gcc install dir>/libexec/gcc/x86_64-redhat-linux/4.9.3"
  for file in ~/binutils2.26/bin/* ; do ln -s "${file}" ; done

之后,~/binutils2.26/bin 中的可执行文件将作为链接复制到 GCC 4.9.3 目录中,并在使用该 GCC 版本构建时自动使用。