Unable to compile Rust hello world on Windows: linker link.exe not found

后端 未结 5 2133
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 08:31

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



        
相关标签:
5条回答
  • 2020-12-13 09:12

    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!
    
    0 讨论(0)
  • 2020-12-13 09:15

    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
    
    0 讨论(0)
  • 2020-12-13 09:19

    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.

    0 讨论(0)
  • 2020-12-13 09:24

    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:

    1. 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:

    1. rustup uninstall toolchain stable-x86_64-pc-windows-msvc
    2. 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)
    3. 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

    0 讨论(0)
  • 2020-12-13 09:26

    Try using Powershell outside Visual Studio, instead.

    Then cargo run in src's parent folder.

    You can try also: rustc

    Good luck.

    0 讨论(0)
提交回复
热议问题