LoginSignup
8
8

More than 3 years have passed since last update.

Windows10+Rustupで環境構築

Last updated at Posted at 2020-01-26

Windows10でGit for windowsのbashを使い、x86_64-pc-windows-msvcツールチェインを有効にする。

環境

  • Windows10 10.0.19551
  • Git for Windows 2.21.0
  • Visual Studio Build Tools 2017 15.9.28307.1000
  • rustup 1.21.1 (7832b2ebe 2019-12-20)
  • rustc 1.40.0 (73528e339 2019-12-16)
  • cargo 1.40.0 (bc8e4c8be 2019-11-22)

Visual Studio Build Toolsをインストール

WindowsでRustを使う場合、MSVC ABIまたはGNU ABI用のバイナリをビルドするため、C++ビルドツールのインストールが必要になる。ここでは前者のVisual Studio Build Toolsを使うことにする。私は下記のVS2017用のビルドツールを使った。

「Visual C++ Build Tools」を選択してインストール・再起動する。

コメント 2020-01-26 210912.png

rustupを実行

Bash for WindowsのBashターミナルを開いて curl https://sh.rustup.rs -sSf | sh を実行する。
C:\Users\ユーザ名.cargo\bin に実行ファイルをインストールして環境変数も設定してくれる模様。

sunas@vrwrhg MINGW64 /
$ curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to
Cargo's bin directory, located at:

  C:\Users\sunas\.cargo\bin

This can be modified with the CARGO_HOME environment variable.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  C:\Users\sunas\.rustup

This can be modified with the RUSTUP_HOME environment variable.

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

default host tripleのターゲットはx86_64-pc-windows-msvcが表示されている。
そのままEnterを入力するとインストールが始まる。

>1

info: profile set to 'default'
info: default host triple is x86_64-pc-windows-gnu
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: latest update on 2019-12-19, rust version 1.40.0 (73528e339 2019-12-16)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-mingw'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-mingw'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'rustfmt'
info: default toolchain set to 'stable'

  stable installed - rustc 1.40.0 (73528e339 2019-12-16)


Rust is installed now. Great!

To get started you need Cargo's bin directory (%USERPROFILE%.cargo\bin) in your
PATH
environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.

Press the Enter key to continue.

インストールが進むが、default host tripleが先ほど表示された「x86_64-pc-windows-msvc」ではなく「x86_64-pc-windows-gnu」になっている。

ビルドテスト(失敗)

ターミナルを開きなおしてサンプルプロジェクトをビルドしてみる。正しく動けば「Hello, World!」が出力されるはずだが…

$ cargo new first_sample
     Created binary (application) `first_sample` package

$ cd first_sample/

$ cargo run
   Compiling first_sample v0.1.0 (C:\Users\sunas\usr\src\rust\first_sample)
error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `first_sample`.

To learn more, run the command again with --verbose.

やはりエラーとなっている。標準ライブラリのstdクレートが使えていない。
rustupがインストール中にx86_64-pc-windows-gnuをターゲットにしているのが問題と思われるため、手動で修正する。

rustup targetでターゲットを再設定

rustup の使用法を確認。
listサブコマンドで現在使用可能なターゲットが確認できる。

$ rustup target --help
rustup.exe-target
Modify a toolchain's supported targets

USAGE:
    rustup.exe target <SUBCOMMAND>

FLAGS:
    -h, --help    Prints help information

SUBCOMMANDS:
    list      List installed and available targets
    add       Add a target to a Rust toolchain
    remove    Remove a target from a Rust toolchain
    help      Prints this message or the help of the given subcommand(s)

現在の設定を確認する。やはりx86_64-pc-windows-gnuがインストール済になっていて、x86_64-pc-windows-msvcは未インストールになっている。

$ rustup target list
(省略)
x86_64-apple-darwin
x86_64-apple-ios
x86_64-fortanix-unknown-sgx
x86_64-fuchsia
x86_64-linux-android
x86_64-pc-windows-gnu (installed)
x86_64-pc-windows-msvc
(省略)

x86_64-pc-windows-gnuターゲットをremoveする。

$ rustup target remove x86_64-pc-windows-gnu
info: removing component 'rust-std'

x86_64-pc-windows-msvcターゲットをaddする。

$ rustup target add x86_64-pc-windows-msvc
info: downloading component 'rust-std' for 'x86_64-pc-windows-msvc'
info: installing component 'rust-std' for 'x86_64-pc-windows-msvc'

rustup toolchainでMSVC用ツールチェインをインストール

更に、rustupでツールチェインをインストールする。

$ rustup toolchain install stable-msvc
info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
info: latest update on 2019-12-19, rust version 1.40.0 (73528e339 2019-12-16)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'rustfmt'

  stable-x86_64-pc-windows-msvc installed - rustc 1.40.0 (73528e339 2019-12-16)

info: checking for self-updates

さらに、デフォルトのツールチェインをstable-msvcに設定する。

$ rustup default stable-msvc
info: using existing install for 'stable-x86_64-pc-windows-msvc'
info: default toolchain set to 'stable-x86_64-pc-windows-msvc'

  stable-x86_64-pc-windows-msvc unchanged - rustc 1.40.0 (73528e339 2019-12-16)

ビルドテスト(成功)

再度ビルドしたところ、成功。

$ cargo run
   Compiling first_sample v0.1.0 (C:\Users\sunas\usr\src\rust\first_sample)
    Finished dev [unoptimized + debuginfo] target(s) in 2.22s
     Running `target\debug\first_sample.exe`
Hello, world!

rustupのバージョンが変わると後半の手順は不要となるかも。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8