読者です 読者をやめる 読者になる 読者になる

エンジニアですよ!

頑張れ俺くん、巨匠と呼ばれるその日まで

Rust メモ Windows向けにクロスコンパイル

Windowsでも使いたいコードがあり、Linux上でWindows向けにクロスコンパイルをしてみたメモ。 rustupを使うと簡単にクロスコンパイルができる。

windows向けのtargetを探す

 $ rustup target list | grep win
i586-pc-windows-msvc
i686-apple-darwin
i686-pc-windows-gnu
i686-pc-windows-msvc
x86_64-apple-darwin
x86_64-pc-windows-gnu
x86_64-pc-windows-msvc

今回は x86_64-pc-windows-gnu を追加する

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

これを --target にしてビルドするが、その前に mingw-w64 をインストール

 $ sudo apt install mingw-w64
Unpacking gcc-mingw-w64-base (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gcc-mingw-w64-i686.
Preparing to unpack .../gcc-mingw-w64-i686_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking gcc-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package g++-mingw-w64-i686.
Preparing to unpack .../g++-mingw-w64-i686_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking g++-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package mingw-w64-x86-64-dev.
Preparing to unpack .../mingw-w64-x86-64-dev_3.1.0-1_all.deb ...
Unpacking mingw-w64-x86-64-dev (3.1.0-1) ...
Selecting previously unselected package gcc-mingw-w64-x86-64.
Preparing to unpack .../gcc-mingw-w64-x86-64_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking gcc-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package g++-mingw-w64-x86-64.
Preparing to unpack .../g++-mingw-w64-x86-64_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking g++-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package g++-mingw-w64.
Preparing to unpack .../g++-mingw-w64_4.8.2-10ubuntu2+12_all.deb ...
Unpacking g++-mingw-w64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gcc-mingw-w64.
Preparing to unpack .../gcc-mingw-w64_4.8.2-10ubuntu2+12_all.deb ...
Unpacking gcc-mingw-w64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gfortran-mingw-w64-i686.
Preparing to unpack .../gfortran-mingw-w64-i686_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking gfortran-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gfortran-mingw-w64-x86-64.
Preparing to unpack .../gfortran-mingw-w64-x86-64_4.8.2-10ubuntu2+12_amd64.deb ...
Unpacking gfortran-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gfortran-mingw-w64.
Preparing to unpack .../gfortran-mingw-w64_4.8.2-10ubuntu2+12_all.deb ...
Unpacking gfortran-mingw-w64 (4.8.2-10ubuntu2+12) ...
Selecting previously unselected package gnat-mingw-w64-base.
Preparing to unpack .../gnat-mingw-w64-base_4.8.2-12ubuntu2+12.1_amd64.deb ...
Unpacking gnat-mingw-w64-base (4.8.2-12ubuntu2+12.1) ...
Selecting previously unselected package gnat-mingw-w64-i686.
Preparing to unpack .../gnat-mingw-w64-i686_4.8.2-12ubuntu2+12.1_amd64.deb ...
Unpacking gnat-mingw-w64-i686 (4.8.2-12ubuntu2+12.1) ...
Selecting previously unselected package gnat-mingw-w64-x86-64.
Preparing to unpack .../gnat-mingw-w64-x86-64_4.8.2-12ubuntu2+12.1_amd64.deb ...
Unpacking gnat-mingw-w64-x86-64 (4.8.2-12ubuntu2+12.1) ...
Selecting previously unselected package gnat-mingw-w64.
Preparing to unpack .../gnat-mingw-w64_4.8.2-12ubuntu2+12.1_all.deb ...
Unpacking gnat-mingw-w64 (4.8.2-12ubuntu2+12.1) ...
Selecting previously unselected package mingw-w64.
Preparing to unpack .../mingw-w64_3.1.0-1_all.deb ...
Unpacking mingw-w64 (3.1.0-1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up binutils-mingw-w64-i686 (2.23.52.20130620-1ubuntu1+3build1) ...
Setting up binutils-mingw-w64-x86-64 (2.23.52.20130620-1ubuntu1+3build1) ...
Setting up mingw-w64-common (3.1.0-1) ...
Setting up mingw-w64-i686-dev (3.1.0-1) ...
Setting up gcc-mingw-w64-base (4.8.2-10ubuntu2+12) ...
Setting up gcc-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Setting up g++-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Setting up mingw-w64-x86-64-dev (3.1.0-1) ...
Setting up gcc-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Setting up g++-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Setting up g++-mingw-w64 (4.8.2-10ubuntu2+12) ...
Setting up gcc-mingw-w64 (4.8.2-10ubuntu2+12) ...
Setting up gfortran-mingw-w64-i686 (4.8.2-10ubuntu2+12) ...
Setting up gfortran-mingw-w64-x86-64 (4.8.2-10ubuntu2+12) ...
Setting up gfortran-mingw-w64 (4.8.2-10ubuntu2+12) ...
Setting up gnat-mingw-w64-base (4.8.2-12ubuntu2+12.1) ...
Setting up gnat-mingw-w64-i686 (4.8.2-12ubuntu2+12.1) ...
Setting up gnat-mingw-w64-x86-64 (4.8.2-12ubuntu2+12.1) ...
Setting up gnat-mingw-w64 (4.8.2-12ubuntu2+12.1) ...
Setting up mingw-w64 (3.1.0-1) ...

そしてビルドするとエラーが出る。こんな感じで。

 $ cargo build --target x86_64-pc-windows-gnu
   Compiling ... v0.1.0 ( )
error: linking with `gcc` failed: exit code: 1
  |
  = note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-nostdlib" "-m64" "..." "..." "-L" "..." "..." "-o" "..." "-Wl,--gc-sections" "-nodefaultlibs" "-L" "..." "-L" "..." "-Wl,-Bstatic" "-Wl,-Bdynamic" "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "-l" "ws2_32" "-l" "userenv" "-l" "shell32" "-l" "advapi32" "-l" "gcc_eh" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "..."
  = note: /usr/bin/ld: unrecognized option '--enable-long-section-names'
/usr/bin/ld: use the --help option for usage information
collect2: error: ld returned 1 exit status

error: aborting due to previous error

error: Could not compile `dumpdir`.

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

既知の問題のようだ。

https://github.com/rust-lang/rust/issues/32859

ひとまずこれで回避できるとのこと。

 $ cat .cargo/config
[target.i686-pc-windows-gnu]
linker = "i686-w64-mingw32-gcc"

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"

できた

 $ cargo build --target x86_64-pc-windows-gnu
   Compiling .... v0.1.0 (file:///.....)
    Finished debug [unoptimized + debuginfo] target(s) in 0.19 secs

補足

各種バージョン情報

 $ rustup --version
rustup 0.6.5 (88ef618 2016-11-04)
 $ rustc --version
rustc 1.13.0 (2c6933acc 2016-11-07)
 $ cargo --version
cargo 0.13.0-nightly (eca9e15 2016-11-01)

 $ uname -a
Linux workspace 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
 $ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"

x86_64-pc-windows-msvc の情報

www.chriskrycho.com