Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust installation on Windows

Tags:

windows

gcc

rust

I was trying to get Rust to work on my Windows box. I followed the instructions, however when I tried hello world program, it spits the below. It looks like it has not been able to link up to gcc.. and I have gcc out there. Could someone help me figure out what is the issue here?

$ rustc  foo.rs

error: linking with `gcc` failed: exit code: 1
note: gcc arguments: '-m32' '-LC:\Program Files (x86)\Rust\bin\rustlib\i686-pc-m
ingw32\lib' '-o' 'foo' 'foo.o' '-shared-libgcc' '-LC:\MinGW\msys\1.0\home\ENwank
wo\.rust' '-LC:\MinGW\msys\1.0\home\ENwankwo' 'C:\Program Files (x86)\Rust\bin\r
ustlib\i686-pc-mingw32\lib\libstd-3e5aeb83-0.9.rlib' 'C:\Program Files (x86)\Rus
t\bin\rustlib\i686-pc-mingw32\lib\libgreen-83b1c0e5-0.9.rlib' 'C:\Program Files
(x86)\Rust\bin\rustlib\i686-pc-mingw32\lib\librustuv-2ba3695a-0.9.rlib' '-lws2_3
2' '-lpsapi' '-liphlpapi' '-lmorestack'
note: gcc: fatal error: -fuse-linker-plugin, but liblto_plugin-0.dll not found
compilation terminated.

error: aborting due to previous error
task 'rustc' failed at 'explicit failure', C:\bot\slave\dist2-win\build\src\libs
yntax\diagnostic.rs:75
task '<main>' failed at 'explicit failure', C:\bot\slave\dist2-win\build\src\lib
rustc\lib.rs:453
like image 666
user618677 Avatar asked Jan 17 '14 21:01

user618677


2 Answers

This is how to install Rust on windows with Visual Studio Code and optionally MSYS2 MinGW

You can Instal Rust without installing "Microsoft C++ Build Tools". You can also use Rust with the gcc or clang tools from the MSYS2 suite.

I didn't want to install Rust on Windows with the required "Microsoft C++ Build Tools" because this would need too much disk space. To get Microsoft C++ Build Tools on Windows you need to download a full Windows SDK which is very large (I think more than 4GB).

You can run and debug Rust programs without installing MSYS2.

If you want to use the gcc or clang compilers that are included in MSYS2 (you will need to install them in a separate step from the MSYS2 installation, using the version of the pacman package manager from MSYS2), then you can install MSYS2.

(MSYS2 also allows you build very easily, C++ programs inside Visual Studio Code with the C++ extensions installed, using the gcc or clang compilers).

The MSYS2 installation version needs 1.61 GB for MSYS2 ( The tools installed using mingw-w64-x86_64-toolchain occupy 1083.49 MiB of disk space out of the 1.61 for the total MSYS2 suite).

Rust installs about 1.46 GB of files - mostly in the "C:\Users\user\.rustup" folder when using the parameter "x86_64-pc-windows-gnu" (for a MSYS MinGW install).

Rust takes up 900 MB after installation, when using "x86_64-pc-windows-msvc" (for a "Microsoft C++ Build Tools" install).

For a better Rust experience inside VS Code it is recommended to install the rust-analyzer extension Online from the VS Code Marketplace, or instead of it, the Rust plugin from [Link1] below, at the bottom of the post.

All you have to do is:

  • Install Visual Studio Code
  • (Optional - Install MSYS2)
  • Install Rust
  • Install Pyton if you don't have it
  • Add the required folders to the PATH variable
  • Install rust-analyzer and CodellDB extensions inside VS Code

At the end of this guide you should be able to run and debug rust programs inside Visual Studio Code.

MSYS2 is optional, you can install it if you want to use the gcc or clang from the suite - after creating the cargo config file, the cargo build and cargo run commands will use msys64\mingw64\bin\clang++.exe (by default if both compilers are installed, or ..\msys64\mingw64\bin\gcc.exe if you set it up to do this).


Just follow the installation from Part 1 - below. (This part is just for reference - it lists the files and folders required for setup and how I chose the installation directories - )

I installed the programs inside these folders:

   D:\Applications\msys64
   D:\Applications\VSCode

rustup-init.exe installs the .cargo folder in the default location:

C:\Users\user\.cargo


(  The settings files for VS Code are created and edited inside the Visual Studio Code editor, you don't need to open the files form windows explorer:
   C:\Users\user\AppData\Roaming\Code\User\settings.json
   C:\Users\user\AppData\Roaming\Code\User\keybindings.json
)

Part 1 - Installation Steps with Details:

I will use the code needed to run the installation programs inside "quotes" - remove the quotes when you run the commands. For example: "pacman -Syu" - remove the quotes and just run pacman -Syu .

Change the paths in the tutorial, for your installations.

The programs needed are VS Code, MSYS2 (MSYS2 has files/tools from MinGW), the rust-analyzer extension for VS Code for code completion and the CodeLLDB extension (with the author Vadim Chugunov) for debugging.

Make sure to have the Windows Firewall setup to allow connections out for MSYS2, VS Code, the Rust installer - rustup-init.exe (maybe rustup later on - during this tutorial you don't need it) and D:\Applications\msys64\usr\bin\pacman.exe. This will allow the programs to download necessary files for components or plugins.

a) Download Visual Studio Code portable version for Windows from filehorse.com and extract the files (or install it any way you like). Open VS Code and disable the 2 telemetry options from the settings:

Click on Edit > Preferences > Settings, type telemetry in the input box and disble the 2 telemetry options (or press the key combo: Ctrl + Shift + P, type open settings and press Enter to open the 'settings.json' file) this will put inside the "settings.json" file this code, between the main open and close curly braces:

"telemetry.enableCrashReporter": false,
"telemetry.enableTelemetry": false

Good fonts to use inside VS Code are: Fira Code, Consolas, DejaVu Sans Mono, Droid Sans Mono Slashed, Inconsolata-g, Bitstream Vera Sans Mono , Lucida Console, maybe Menlo Regular (was used in MacOS), Meslo LG DZ.

Fonts like 'Fira Code' have font ligatures present inside the font. 'Fire Code' doesn't have traditional font ligatures for example ft is not connected, but has ligatures specific for coding, for example the look of these character combinations are changed and usually look better with ligatures enabled: fl , -> , <= , >= . See the image below the post for an image with font ligatures on vs off. To enable font ligatures, open the Preferences for Visual Studio Code, search for ligatures in the search box, and enable the setting: 'Editor:Font Ligatures' .

You can change the default user data directory from:

"C:\Users\user\AppData\Roaming\Code" to your desired location, e.g. :

"D:\Applications\VSCode\UserData\Code"

by creating a shortcut for VSCode with the parameter --user-data-dir, that you'll use to launch VS Code.

To do this create a shortcut for VSCode with the setting for Target: D:\Applications\VSCode\Code.exe --user-data-dir "D:\Applications\VSCode\UserData\Code"

b) This step is Optional, follow it only if you want to use gcc or clang from MSYS2:

You can run Rust programs and compile them in Visual Studio Code without installing MSYS2. Install MSYS2 only if you want to use its gcc or clang compiler.

Optional - Install MSYS2 - this is about 1.61 GB. Download MSYS2 and follow the installation instructions - I will mention the most important steps:

  • Install MSYS2 and keep the checkbox selected: "run MSYS2 64bit" at the end of the installation.

  • Run pacman -Syu in the console that appears after the installation. This is the MSYS2 shell.

!!! For the next step, be careful an choose the right shortcut Don't choose the MinGW one, so

  • run the shortcut named "MSYS2 MSYS" from Start menu (it is "D:\Applications\msys64\msys2_shell.cmd -msys"). This is the MSYS2 shell. Inside the shell run pacman -Su .

    The next step is to download the programs needed to build the programs written in Rust, for the Windows Platform:

  • From the start menu run "MSYS2 MinGW 64-bit" (it is "D:\Applications\msys64\msys2_shell.cmd -mingw64"). This is for MinGW and it has a different parameter from the shortcut used above.

  • Run pacman -S mingw-w64-x86_64-toolchain.

Press Enter, Enter and type 'y' during the installation process.

( The guide on the msys2.org page said to also install some packages using the option '--needed base-devel' by runnig the command "pacman -S --needed base-devel mingw-w64-x86_64-toolchain", but I don't think they are needed here.

If you want, you can install single tools and their dependencies from the list of tools installed by the command "--needed base-devel", by selecting their corresponding number during the installation, for example you can just install make and not the rest of the tools present in the "--needed base-devel" package.

The simple rust "hello world" program that I wrote in rust using VS Code and the rustc and cargo tools/programs ran well without installing the "--needed base-devel" files.

The --needed base-devel files are not needed to run and debug simple Rust programs,but you can install them if you want.

To use the gcc or clang linker and compiler from MSYS2 you have to create the C:\Users\user\.cargo\config file:

To make the cargo use clang++.exe , add these lines to the C:\Users\user\.cargo\config file:

[target.x86_64-pc-windows-gnu]
linker = "D:\\Applications\\msys64\\mingw64\\bin\\clang++.exe"
ar = "D:\\Applications\\msys64\\mingw64\\bin\\llvm-ar.exe"

If you want to change the linker to gcc, create the C:\Users\user\.cargo\config file that contains the lines:

[target.x86_64-pc-windows-gnu]
linker = "D:\\Applications\\msys64\\mingw64\\bin\\gcc.exe"
ar = "D:\\Applications\\msys64\\mingw64\\bin\\ar.exe"

(clang++.exe is the same file as clang.exe from the same directory.) I got everything installed and running well - I could run the programs and debug them using gcc or clang. )

c) Install Rust for Windows Download the installer: rustup-init.exe from rust-lang.org

The browser should recognize that you ar using Windows and give you the link to "DOWNLOAD RUSTUP-INIT.EXE (64-BIT)" , that is: [...]static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

Run rustup-init.exe . It is better to launch a console with cmd.exe and launch rustup-init.exe from there.
  • choose "Continue? (y/N)" by typing y and hitting the Enter key.

  • choose "2) Customize installation" using the keyboard.

  • paste or type the option: x86_64-pc-windows-gnu .

  • Press enter for the rest of the options.

  • Finally type "1" as input in the console, then press Enter, to choose the option "1 to proceed" with the Current installation Options.

There might be problems if you had rustup already installed. If you already have rustup installed follow the indications on the guide at Source 1 Link at the bottom of the page with the step-by-step-instruction-to-install-rust-and-cargo-for-mingw-with-msys2 :

This is a quote from that post:

"Note: If rustup is already installed, then rerunning rustup-init won't actually install the requested toolchain. Instead, run rustup toolchain install stable-x86_64-pc-windows-gnu if you already have the MSVC-based toolchain. Then run rustup default stable-x86_64-pc-windows-gnu to set the GNU-based toolchain as the default."

d) Add the folders to the PATH

Add the folders that have useful programs from rust and msys2 to the Windows system PATH variable:

Adding the folders to the system PATH variable will allow VS Conde console, which defaults to PowerShell (you can change it to Command Prompt or other shell if you want), to recognize the commands: cargo, rustc, etc. This will make running and debugging rust programs in VS Code with the rust-analyzer and CodeLLDB extensions easy because you don't have to configure anything else.

To add the folders to the PATH use the System Properties window:

  • Right click on "My Computer" and select "properties"
  • Click on Advanced System Settings
  • In the Advanced tab, click on "Environment Variables"
  • under 'User variables for user' click on the PATH Variable and add the locations of your cargo\bin folder and mingw64\bin folder, (plus \msys64):
;C:\Users\user\.cargo\bin;D:\Applications\msys64;D:\Applications\msys64\mingw64\bin

This will add the folders "C:\Users\user\.cargo\bin", "D:\Applications\msys64", "D:\Applications\msys64\mingw64\bin" to the PATH variable. (stackoverflow is bad at formatting \. backslash followed by a dot - you have to use another backslash as an escape character)

To test that the PATH variable is updated open a NEW Command Prompt or PowerShell window, and in this window run cargo --version or gcc --version (if you installed MSYS2 and gcc.exe) or rustc --version inside a 'Command Prompt' window and inside a PowerShell Window. If the commands are not found, restart your computer so that Windows can update the PATH variable. For me the change worked without having to restart, I just had to open a new shell window.

e) Install the Rust extensions for VS Code

rust-analyzer and CodeLLDB require Python to be installed.

Install Python 3.6.3, for example, and select to add it to the path during installation.

From the VS Code Marketplace install the needed extensions. Open VS Code, click on the Extensions button on the right toolbar, and search and install the extensions:

  • rust-analyzer - (publisher: matklad) ;
  • CodeLLDB (publisher: Vadim Chugunov);

( I didn't have to do this next step so you shouldn't either - if rust-analyzer doesn't load/run properly, you may need to install the rust source code. Using the 'Command Prompt' run: rustup component add rust-src to install the rust Source Code. I didn't have to install it, the rust-analyzer worked well [ by the way, the VS Code could connect to the internet during testing].

)

[While running Visual Studio Code you may get a message telling you to install .Net Framework 4.5.2. Download and install the Offline version if the online version fails to install. (On Windows 7 without some updates, you may have to download and install the certificate: MicRooCerAut2011_2011_03_22.crt, and install some windows updates: Windows6.1-KB2533623-x64, windows6.1-kb2813430-x64, and maybe Windows6.1-KB4019990-x64 ) ]

Part 2 - Running and Debugging Rust programs with VS Code

Running and compiling in Rust with VS Code and Msys2:

After adding the folders to the PATH and installing the extensions, you basically need to run:

  1. cargo init
  2. cargo run - to run the program.
  3. press the F5 key, or click the debug button to debug the code.

First you need to:

  • Create a folder for your Rust app.
  • Open the folder inside VS Code.
  • Press Ctrl+{`} to open the terminal panel. The default shell will be 'PowerShell'.

You can select the 'Command prompt' shell from the Terminal pane using the down arrow on the left, that is near the plus button and x button. Or you can set VS Code to use 'Command Prompt' as the default shell by adding the lines to the 'settings.json' file - the main Visual Studio Settings file.

"terminal.integrated.defaultProfile.windows": "Command Prompt",

You can use PowerShell or 'Command Prompt', they both work fine.

  • Run cargo init inside the terminal to create the project files.

  • Edit the main.rs file if you want. It is located inside 'app folder'\src\main.rs

  • Run cargo run inside the terminal

If you have rust-analyzer installed, the extension puts 2 buttons as popups on top of the main() function. They are named 'Run' and 'Debug' and if clicked they do what their names say.

To debug, put a breakpoint inside your main.rs file and hit F5 (the menu entry Run > Start Debugging) or click the Debug button on top of the main() function placed there by the rust-analyzer extension if you have it installed.

If you click the debug button you don't have to create a launch.json config file. If you hit the F5 key or click the menu entry Run > Start Debugging, a launch.json file will be added to your folder.

Just click Yes in the message box to auto generate the launch.json file. Then hit the F5 key again to start debugging. Now you can step through your code during the debugging process.


Link 1: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust

Link to Font Ligatures example - Image for the font 'Fira Code':

Image Link: Coding font with font ligatures

like image 181
pclinux Avatar answered Oct 01 '22 14:10

pclinux


This question is old, but in the mean time a lot has changed.

The easiest way to install on Windows is to go to the Rust installation page and download and run rustup-init.exe. It installs rustc, rustup and similar tools. It just works™.

like image 25
Daniel Fath Avatar answered Oct 01 '22 16:10

Daniel Fath