为什么 MSVC 没有安装或被 Rust 识别?

Why is MSVC not installing or being recognized by Rust?

我已经反复尝试过,但仍然无法弄清楚我哪里出错了。 我从官方网站(版本1.44.1)安装了Rust,我可以运行 rustc --version 得到正确的结果。我可以创建一个新项目,但是当我 运行 cargo testcargo build 时,出现错误。

   Compiling hello-rust v0.1.0 (C:\Projects\rust-test\hello-rust)
error: linking with `link.exe` failed: exit code: 1
  |
  = note: "link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.2475ppgeya7982p6.rcgu.o" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.2k8om5gztlm4khis.rcgu.o" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.2npazqbfnne9a4sz.rcgu.o" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.31mw3i7sxv4jy5mf.rcgu.o" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.39lo872ze2g8kjy6.rcgu.o" "/OUT:C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.exe" "C:\Projects\rust-test\hello-rust\target\debug\deps\hello_rust-3bd7255de8fc516c.3pzk8mfjqu22abeg.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\intrinsic.natvis" "/NATVIS:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\liballoc.natvis" "/NATVIS:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\libcore.natvis" "/NATVIS:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\etc\libstd.natvis" "/LIBPATH:C:\Projects\rust-test\hello-rust\target\debug\deps" "/LIBPATH:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libtest-ce9bcda9bff89d22.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libterm-5685a6e382a0344d.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libgetopts-9672beb79727d086.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libunicode_width-0b6119489f498de7.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_std-f43d3b6a5d7adb85.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libstd-13b0a7dd239e134e.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libpanic_unwind-c6983deeb3795666.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libhashbrown-fb1f6de11ac9d69b.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_alloc-09135599e2aa779b.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libbacktrace-4aec9f98c25f9f5d.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_demangle-29671087b5d92459.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libunwind-7c5d84958fafcb64.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcfg_if-c820fe24a63d105a.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liblibc-eab51dcd5481cddb.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\liballoc-da92c312ae6fecdd.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\librustc_std_workspace_core-693c88df9572eb34.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcore-b5aae910499df009.rlib" "C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libcompiler_builtins-9a7454af4f639d0b.rlib" "kernel32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
  = note: /usr/bin/link: extra operand ‘/LIBPATH:C:\Users\Jaden\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib’
          Try '/usr/bin/link --help' for more information.


error: aborting due to previous error

error: could not compile `hello-rust`.

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

我的研究发现了有关 MSVC 未正确设置或 PATH 变量中缺少某些内容的问题。这个错误是什么意思?我安装了 Visual Studio 2017(只是基础,我没有检查任何工作负载或单个组件),据我所知,这是 MSVC (Microsoft Studio Visual C++)。我是 Rust 和一般低级编码的新手(我是 Python/JavaScript 人),所以我真的不明白我需要在这里做什么。有人可以在这里给我指导吗?

为什么 MSVC 没有安装或被 Rust 识别?什么会导致这个?我该如何解决?

我碰巧自己找到了答案,但我没有在任何地方找到关于 Rust 的文档。我搜索了更多有关安装 MSVC 的信息,并找到了这个 Microsoft 页面:https://docs.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=vs-2019

For core C and C++ support, choose the "Desktop development with C++" workload.

我修改了我的安装并选择了使用 C++ 工作负载的桌面开发。成功了!

当您使用 rustup-init.exe 安装 Rust 时,它应该会自动检测您是否安装了 Microsoft C++ 构建工具。如果它们没有安装,它会在第一时间给你一个详细的信息,告诉你如何安装它们。

这是 Windows 10:

上的样子
Rust Visual C++ prerequisites

Rust requires the Microsoft C++ build tools for Visual Studio 2013 or
later, but they don't seem to be installed.

The easiest way to acquire the build tools is by installing Microsoft
Visual C++ Build Tools 2019 which provides just the Visual C++ build
tools:

  https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

Please ensure the Windows 10 SDK and the English language pack components
are included when installing the Visual C++ Build Tools.

Alternately, you can install Visual Studio 2019, Visual Studio 2017,
Visual Studio 2015, or Visual Studio 2013 and during install select
the "C++ tools":

  https://visualstudio.microsoft.com/downloads/

Install the C++ build tools before proceeding.

If you will be targeting the GNU ABI or otherwise know what you are
doing then it is fine to continue installation without the build
tools, but otherwise, install the C++ build tools before proceeding.

Continue? (Y/n)