I have installed Rust on windows from Rust installation page. After installation I tried running the \"hello world\" program but got the following error.
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!
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 had the same issue and found it to be present even after installing the Build Tools. What I realized almost by accident that I was running all my cargo commands in "Developer Command Prompt for Visual Studio ". Running the same commands in a simple cmd shell ran without any issues.
What worked for me: Running the command prompt directly and not use the shortcuts created by Visual Studio.
Possible Cause: Visual Studio Command Prompt runs bat files e.g. VsDevCmd.bat before it starts the shell (to load VS related environment variables, etc.) and possibly one of the commands in that file screws up the path cargo uses to get to linker.
Someone could dig further to find the exact line that causes the issue if they really want to know.
Download the Visual Studio 2019 Build tools from the Microsoft website: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16
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
Try using Powershell outside Visual Studio, instead.
Then cargo run in src's parent folder.
You can try also: rustc
Good luck.