Unable to compile Rust hello world on Windows: lin

2020-02-19 00:10发布

I have installed Rust on windows from Rust installation page. After installation I tried running the "hello world" program but got the following error.

>cargo run

Error

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.

Code:

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

2条回答
时光不老,我们不散
2楼-- · 2020-02-19 01:07

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.

查看更多
干净又极端
3楼-- · 2020-02-19 01:09

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!
查看更多
登录 后发表回答