Cross-compile a Rust application from Linux to Windows

前端 未结 5 554
挽巷
挽巷 2020-12-12 15:02

Basically I\'m trying to compile the simplest code to Windows while I am developing on Linux.

fn main() {
    println!(\"Hello, and bye.\")
}
5条回答
  •  北海茫月
    2020-12-12 15:41

    I've had success on Debian (testing) without using Mingw and Wine just following the official instructions. They look scary, but in the end it didn't hurt that much.

    The official instructions also contain info on how to cross-compile C/C++ code. I haven't needed that, so it's something I haven't actually tested.

    A couple of remarks for individual points in the official instructions. The numbers match the numbers in the official instructions.

    1. Debian: sudo apt-get install lld
    2. Make a symlink named lld-link to lld somewhere in your $PATH. Example: ln -s /usr/bin/lld local_bin/lld-link
    3. I don't cross-compile C/C++, haven't used this point personally.
    4. This is probably the most annoying part. I installed Rust on a Windows box via rustup, and copied the libraries from the directories named in the official docs to the Linux box. Beware, there were sometimes uppercase library filenames, but lld wants them all lowercase (Windows isn't case-sensitive, Linux is). I've used the following to rename all files in current directory to lowercase:

      for f in `find`; do mv -v "$f" "`echo $f | tr '[A-Z]' '[a-z]'`"; done
      

      Personally, I've needed both Kit directories and just one of the VC dirs.

    5. I don't cross-compile C/C++, haven't used this point personally.
    6. Just make $LIB_ROOT in the script at the end of this post point to the lib directory from point 3.
    7. Mandatory
    8. I don't cross-compile C/C++, haven't used this point personally.
    9. Depending the target architecture, either of the following:
      • rustup target add i686-pc-windows-msvc
      • rustup target add x86_64-pc-windows-msvc

    For cross-building itself, I'm using the following simple script (32-bit version):

    #!/bin/sh
    # "cargo build" for the 32-bit Windows MSVC architecture.
    
    # Set this to proper directory
    LIB_ROOT=~/opt/rust-msvc
    
    # The rest shouldn't need modifications
    VS_LIBS="$LIB_ROOT/Microsoft Visual Studio 14.0/VC/lib/"
    KIT_8_1_LIBS="$LIB_ROOT/Windows Kits/8.1/Lib/winv6.3/um/x86/"
    KIT_10_LIBS="$LIB_ROOT/Windows Kits/10/Lib/10.0.10240.0/ucrt/x86/"
    export LIB="$VS_LIBS;$KIT_8_1_LIBS;$KIT_10_LIBS"
    cargo build --target=i686-pc-windows-msvc "$@"
    

    I'm using the script the same way I would use cargo build

    Hope that helps somebody!

提交回复
热议问题