OS: Fedora 33 (Workstation Edition) x86_64
CPU: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz x86_64
rustc: 1.49.0 (e1884a8e3 2020-12-2)
cargo: 1.49.0 (d00d64df9 2020-12-05)


# Install build dependencies

sudo dnf install -y mingw64-gcc
sudo dnf install -y mingw64-winpthreads-static

# Add Windows Rust target
rustup target add x86_64-pc-windows-gnu
rustup toolchain install stable-x86_64-pc-windows-gnu


cargo new hello
cd hello
cargo build --target x86_64-pc-windows-gnu

交叉编译默认的hello world, 需要链接 libpthread.a, 如果上面我们没有安装 mingw64-winpthreads-static 包,则会在链接时产生一个错误:

❯ cargo build --target x86_64-pc-windows-gnu
   Compiling hello v0.1.0 (/tmp/hello/hello)
error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
  = note: /usr/lib/gcc/x86_64-w64-mingw32/10.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -l:libpthread.a
          collect2: error: ld returned 1 exit status

error: aborting due to previous error

error: could not compile `hello`

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


❯ sudo dnf search mingw64 | rg pthread
Last metadata expiration check: 0:57:48 ago on Mon 25 Jan 2021 01:43:34 AM CST.
mingw64-winpthreads.noarch : MinGW pthread library for the win64 target
mingw64-winpthreads-static.noarch : Static version of the MinGW Windows pthreads library

❯ sudo dnf install mingw64-winpthreads-static


gnu vs msvc

Difference between the gnu and msvc toolchains?

The GNU toolchain uses the MinGW build tools (mostly the linker) and produces binaries that use the GNU ABI. The MSVC toolchain uses the Visual Studio build tools and produces binaries that use the Microsoft ABI, making them more compatible with most other Windows binaries/libraries.