【问题标题】:Unable to compile Rust hello world on Windows: linker link.exe not found无法在 Windows 上编译 Rust hello world:未找到链接器 link.exe
【发布时间】:2019-08-31 08:31:14
【问题描述】:

我已经从Rust installation page 在 Windows 上安装了 Rust。安装后我尝试运行“hello world”程序,但出现以下错误。

>cargo run

错误

Compiling helloworld v0.1.0 (C:\Users\DELL\helloworld)

error: linker `link.exe` not found
note: The system cannot find the file specified. (os error 2)
note: the msvc targets depend on the msvc linker but `link.exe` was not found
note: please ensure that VS 2013, VS 2015 or VS 2017 was installed with the Visual C++ option
error: aborting due to previous error
error: Could not compile `helloworld`.

To learn more, run the command again with --verbose.

代码

fn main() {
    println!("Hello, world!");
}

【问题讨论】:

  • 错误信息准确地说明了要做什么:"注意:请确保已使用 Visual C++ 选项安装了 VS 2013、VS 2015 或 VS 2017"
  • 是的,认为它可能对其他人有用,因此与其答案分享了这个问题。
  • @hellow 不太“完全”,这就是整个问题.... Visual Studio 安装程序有大约 30 个选项,安装它们可能需要 TB 的下载量。下面的 filiphagan 很有帮助地提到了必要的内容。
  • @Merk "[...] 是使用 Visual C++ 选项安装的" 我的意思是...它不会告诉您安装所有东西,是吗?
  • @hellow "exactly" 建议“有足够的信息从列表中挑选项目”,考虑到列表中有 Visual C++ 和 VS 20xx 的(多 Gb)选项数量,情况并非如此,正如下面的多个答案所证明的那样。对此问题的有益评论将有助于区分它们。

标签: compiler-errors installation rust linker-errors build-tools


【解决方案1】:

我下载并安装了Build Tools for Visual Studio 2019。在安装过程中,我选择了 C++ 工具。它下载了近 5GB 的数据。安装后我重启了机器,编译代码工作正常:

> cargo run
Compiling helloworld v0.1.0 (C:\Users\DELL\helloworld)
Finished dev [unoptimized + debuginfo] target(s) in 12.05s
  Running `target\debug\helloworld.exe`
Hello, world!

【讨论】:

  • 我们是否需要选中所有默认复选框?否则,为了让 Rust 正常工作,我们可以安装的最小依赖集是多少?
  • 非常感谢。
  • @argenkiwi 我能够使其仅使用“MSVC v142 - VS 2019 C++ x64/x86 构建工具”和“Windows 10 SDK”。
  • 没有其他选择吗?
  • 我只尝试了 C++ 构建工具,但没有成功 - 您还需要 Windows 10 SDK。
【解决方案2】:

我遇到了同样的问题,即使在安装构建工具后也发现它存在。我几乎意外地意识到我正在“Visual Studio 的开发人员命令提示符”中运行所有的货物命令。在简单的 cmd shell 中运行相同的命令没有任何问题。

什么对我有用:直接运行命令提示符而不使用 Visual Studio 创建的快捷方式。

可能的原因:Visual Studio 命令提示符运行 bat 文件,例如VsDevCmd.bat 在启动 shell 之前(加载 VS 相关的环境变量等),并且可能该文件中的命令之一搞砸了 cargo 用于访问链接器的路径。

如果他们真的想知道,可以进一步挖掘以找到导致问题的确切行。

【讨论】:

  • 非常感谢!来自命令行的cargo run(而不是 VS 的命令提示符)对我有帮助。
【解决方案3】:

尝试在 Visual Studio 之外使用 Powershell。

然后 cargo 在 src 的父文件夹中运行。

你也可以试试:rustc

祝你好运。

【讨论】:

    【解决方案4】:

    案例1:使用C++ win编译器,修复需要重新安装VS构建工具C++ 从 Microsoft 网站下载 Visual Studio 2019 构建工具: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16

    下载后,在安装构建工具时,请确保安装所需的组件:

    1. C++ 构建工具

    这将下载所需的文件。成功安装所有内容后,重新启动并重新运行您的 rust 程序,它将成功编译。

    案例2:这个错误可能来自于你使用GCC编译、修复它(假设你已经有MinGW):

    在 cmd 中录音:

    1. rustup uninstall toolchain stable-x86_64-pc-windows-msvc
    2. rustup toolchain install stable-x86_64-pc-windows-gnu (或在 https://forge.rust-lang.org/infra/other-installation-methods.html 下载您选择的平台的 rustup-init)
    3. rustup default stable-x86_64-pc-windows-gnu

    案例 3:您不想下载带有构建工具的 Visual Studio,只需使用 g++ gcc 开发包安装 MinGw,然后运行案例 2

    【讨论】:

    • 这对我有用 非常感谢,我不必下载 1.2GB 的东西。
    • 当然!我很乐意提供帮助。
    • rustup uninstall toolchain stable-x86_64-pc-windows-msvc 这个命令抛出访问被拒绝错误,不确定有多少人遇到这个问题。虽然这个答案是一个非常详细的答案,但我投票支持以下答案,因为它更多的是快速修复。
    • 谢谢!解决了这个问题。我以某种方式误解了 rustup 关于需要 VC++ 构建工具的多个警告。就像它是可选的一样,呵呵
    【解决方案5】:

    我遇到了类似的问题“error: linking with link.exe failed: exit code: 1

    为了解决这个问题,我做到了

    rustup toolchain install stable-x86_64-pc-windows-gnu
    

    然后

    rustup default stable-x86_64-pc-windows-gnu
    

    cargo build
      Compiling hello v0.1.0 (C:\Users\leke\dev\rust\hello)
        Finished dev [unoptimized + debuginfo] target(s) in 1.66s
    

    【讨论】:

    • 这不是一种解决方案,而是一种变通方法,重要的是要考虑到这样做时会更改工具链并且有副作用。
    • 对于 Windows 32 位系统,您将需要 "stable-i686-pc-windows-gnu"
    • @LuisAyuso 如果您列出您想到的副作用,您的评论会很有帮助。
    • 这对我有用!安装 MS C++ 工具并没有做任何事情,但之后运行它修复了它。
    【解决方案6】:

    错误消息非常不清楚,因为无需安装 Vistual Studio 即可运行 rust 代码。 “Visual C++ 选项”到底是什么意思? “VS 2013、VS 2015 或 VS 2017”也是错误的 - 无需安装这些特定版本的完整 Visual Studio。

    要执行“cargo run”,您需要从 Build Tools for Visual Studio 安装 C++ 构建工具。 2019版就好了。下载链接: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16#

    在安装过程中不仅要选择默认的“包含”C++ 工具,还要选择三个'optional' C++ building tools:MSVC(...)、Windows 10 SDK、适用于 Windows 的 C++ CMake 工具。

    【讨论】:

    • 这个答案与currently accepted answer 有何不同?链接是相同的,并且 cmets 提供了所需的最少安装选项。
    • @kmdreko 答案没有解决问题,因为那里没有提到 MSVC 和 Windows 10 SDK 等其他要求
    【解决方案7】:

    我的系统变量中有一些来自旧 Visual Studio 安装的变量。删除那些解决了问题。

    VCHOME            C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
    VCINSTALLDIR      C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
    VS140COMNTOOLS    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common Tool...
    vsinstalldir      C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
    

    【讨论】:

      【解决方案8】:

      首先,下载Microsoft C++ Build Tools并安装。 然后,安装rustup-init.exe。 并且安装成功后不要删除第一个。

      【讨论】:

      • 除了已接受的答案(已经解释了安装构建工具)之外,还不清楚这提供了什么。
      【解决方案9】:

      C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64 添加到 PATH 变量就可以了

      【讨论】:

        【解决方案10】:

        如果上述解决方案仍然不适合您(现在是 2021 年),Rust 使用 msvcgnu 编译器,因此您可以随时切换到 gnu 编译器:

        $ rustup default stable-x86_64-pc-windows-gnu
        

        【讨论】:

        • 完美!这可以防止更改公司的主要开发环境!
        【解决方案11】:

        好的!

        这正是我所做的。转到https://visualstudio.microsoft.com/visual-cpp-build-tools/,它将下载 Visual Studio 安装程序。

        以管理员身份运行它,然后确保下载屏幕截图中列出的所有三个内容,版本无关紧要,尝试获取最新版本。

        点击安装。重新启动计算机。不客气。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-25
          • 1970-01-01
          • 2019-01-17
          • 2012-01-20
          • 2014-11-29
          相关资源
          最近更新 更多