2022年5月24日 星期二

Rust 的“error: linker `link.exe` not found”錯誤

這是照著官網的新手入門,一步一步來時發生的問題。

照著官網建立hello-rust專案後,執行cargo run,就會跳出以下錯誤(我的環境是 win10)

C:\hello-rust> cargo run
   Compiling hello-rust v0.1.0 (C:\hello-rust)
error: linker `link.exe` not found
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017, VS 2019 or VS 2022 was installed with the Visual C++ option

error: could not compile `hello-rust` due to previous error

上網查了一下,這是因為 rust 會需要使用到 C/C++ 的compiler。

默認上會使用到 stable-x86_64-pc-windows-msvc,見下

C:\> rustup default
stable-x86_64-pc-windows-msvc (default)

可是stable-x86_64-pc-windows-msvc是基於 Microsoft Visual C++ 的,換言之,我沒有安裝 Microsoft Visual C++ 就不能使用。

解法

安裝 Microsoft Visual C++

解決方案之一,就是安裝 Microsoft Visual C++,可以選擇只安裝 Visual Build Tools 而不安裝 Visual Stdio IDE

兩者的下載網址都是https://visualstudio.microsoft.com/zh-hant/downloads

如果只要下載 Build Tools,在下方的搜尋欄輸入 然後搜尋即可找到。

如果不想安裝 Microsoft Visual C++ ,也可以改安裝 MinGW。

更換 Tool Chain

第二種解法式更換 toolchain ,如下所示:

C:\>rustup toolchain install stable-x86_64-pc-windows-gnu
C:\> rustup default stable-x86_64-pc-windows-gnu

安裝完後就可以執行 rust 了

參考資料

(2019, October 4). compiler errors - Unable to compile Rust hello world on Windows: linker link.exe not found. Stackoverflow. https://stackoverflow.com/questions/55603111/unable-to-compile-rust-hello-world-on-windows-linker-link-exe-not-found
悟初境,  (2021, December 18). error: linker `link.exe` not found. 悟初境的博客. https://blog.csdn.net/jimo_lonely/article/details/122013975
F001, 为什么Rust这样的语言还需要C/C++编译器?. 知乎. https://www.zhihu.com/question/266347224

沒有留言:

張貼留言