Unable to compile Rust hello world on Windows: linker link.exe not found
The error message is very unclear because there is no need to have Vistual Studio installed to run rust code. And what does "Visual C++ option" mean exactly? "VS 2013, VS 2015 or VS 2017" is also wrong - there's no need to install the full Visual Studio of these particular versions.
To execute 'cargo run' you need to install C++ build tools from Build Tools for Visual Studio. Version 2019 is just fine. Download link: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16#
It's important to select not only the default 'included' C++ tools during the installation but also three 'optional' C++ building tools: MSVC(...), Windows 10 SDK, C++ CMake tools for Windows.
Case 1: Using C++ win compiler, to fix it you need to reinstall VS build tool C++ Download the Visual Studio 2019 Build tools from the Microsoft website: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16
After the download, while installing the Build tools, make sure that you install the required components:
- C++ build tools
This will download required files. Once everything is successfully installed, reboot and re-run your rust program and it will compile successfully.
Case2: This error can come from the fact that you use GCC to compile, to fix it (assume that you already have MinGW):
Tape in cmd:
rustup uninstall toolchain stable-x86_64-pc-windows-msvc
rustup toolchain install stable-x86_64-pc-windows-gnu
(or download rustup-init for the platform of your choice at https://forge.rust-lang.org/infra/other-installation-methods.html)rustup default stable-x86_64-pc-windows-gnu
Case 3: You don't want to download Visual studio with build tools, simply install MinGw with g++ gcc development packages, then run CASE 2
I had a similar issue "error: linking with link.exe failed: exit code: 1
"
To solve it, I did
rustup toolchain install stable-x86_64-pc-windows-gnu
then
rustup default stable-x86_64-pc-windows-gnu
and
cargo build
Compiling hello v0.1.0 (C:\Users\leke\dev\rust\hello)
Finished dev [unoptimized + debuginfo] target(s) in 1.66s
I downloaded and installed the Build Tools for Visual Studio 2019. During installation I selected the C++ tools. It downloaded almost 5GB of data. I restarted the machine after installation and compiling the code worked fine:
> 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!