如何在Linux上修复Rust错误“找不到链接器'cc'”

时间:2020-03-21 11:44:34  来源:igfitidea点击:

今天,我正在Ubuntu虚拟机上测试一种称为Bandband的网络带宽利用工具。

该工具是用Rust编程语言开发的,因此我尝试使用Cargo软件包管理器进行安装。

在编译过程中,我遇到以下错误:

Updating crates.io index
Installing bandwhich v0.6.0
Compiling libc v0.2.66
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: aborting due to previous error
error: failed to compile `bandwhich v0.6.0`, intermediate artifacts can be found at `/tmp/cargo-installrqSeTB`
Caused by:
could not compile `libc`.
To learn more, run the command again with --verbose.

从上面的输出中可以看到,Cargo找不到cc编译器程序来编译给定的应用程序。
由于Rust尚不包含自己的链接器,因此我们需要安装诸如gcc之类的C编译器以充当链接器。

要在Ubuntu上安装gcc,只需运行:

$sudo apt install build-essential

如果我们使用的是其他Linux操作系统,请参考以下链接以安装开发工具,其中包括必要的应用程序,例如GNU GCC C/C++编译器,make和调试器等。

安装gcc之后,错误"linker 'cc' not found"消失了!然后,我可以没有任何问题地安装该应用程序。

如果即使已经安装了GCC,仍然出现相同的错误,请安装cmake,然后重试。
在大多数Linux发行版的官方存储库中都可以找到Cmake。

要在Arch Linux上安装Cmake,请启用[Extra]存储库并运行:

$sudo pacman -S cmake

在Debian,Ubuntu和Linux Mint上:

$sudo apt install cmake

在Fedora上:

$sudo dnf install cmake

在CentOS上,RHEL:

$sudo yum install cmake

在openSUSE上:

$sudo zypper install cmake

安装gcc为我解决了问题。

在NixOS中,即使安装了“ GCC”和“ make”,该问题也无法解决。
如该GitHub说明中所述,我们应该使用nix-shell而不是nix-env进行尝试。