iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

Setting up an MSVC build environment for Rust on Windows

に公開

The Build Environment Takes Up Too Much Space

Related Article:
Building a Windows Development Environment with WSL2: Rust on the Move (1)

------------------- ↓ Introduction ↓-------------------

The number of command-line tools written in Rust is increasing year by year.
Recently, I used ferium, a mod management tool for Minecraft.
(Minecraft again, right?)

Setting that aside,
with the increase in command-line tools written in Rust,
comes the need for a command execution environment.
Unless they are distributed as binaries,
you need to build them yourself.
Rust commands, in particular, tend to be distant from Windows.
So, I've decided to set up a Windows build execution environment.

ヾ(・ω<)ノ" 三三三● ⅱⅲ Roll roll♪

------------------- ↓ Main Body ↓-------------------

Installing the Build Environment

There are two versions of the Rust build environment: the GNU version and the MSVC version.
The GNU version runs on Linux-like environments such as Msys,
while the MSVC version is built using Visual C++.
It is better to use the MSVC version for easily creating commands on Windows.

gsudo winget install Rustlang.Rust.MSVC

To build in an MSVC environment with Rust,
you need the Visual Studio C++ libraries.
Instead of installing the entire Visual Studio suite,
I will install only the Build Tools.

gsudo winget install Microsoft.VisualStudio.2022.BuildTools

Launch the "Visual Studio Installer"

Check "Desktop development with C++"

Check "C++/CLI support for vxxx build tools (latest)"

Trying a Build

I'll try building a Rust command that can be installed via the cargo command.
I'll use cargo-update, which manages the versions of installed commands.

cargo install cargo-update
    Updating crates.io index
  Downloaded cargo-update v8.1.4
...
  Installed package `cargo-update v8.1.4` (executables `cargo-install-update-config.exe`, `cargo-install-update.exe`)

Let's try running it

 cargo install-update -a
    Updating registry 'https://github.com/rust-lang/crates.io-index'

Package       Installed  Latest  Needs update
cargo-update  v8.1.4     v8.1.4  No

No packages need updating.
Overall updated 0 packages.

|ω・) It's working

------------------- ↓ Afterword ↓-------------------

Caution Regarding Updates

When installing with winget, you might want to manage the app with winget, but
the rust package is always an additional installation, not an update.
Therefore, you need to uninstall the old package when upgrading.

winget uninstall Rustlang.Rust.MSVC
winget install Rustlang.Rust.MSVC

If you accidentally perform a duplicate installation,
it cannot be controlled by the winget command,
so please remove the old one from "Add or remove programs".

Discussion