Cross-compile a Rust application from Linux to Windows

前端 未结 5 574
挽巷
挽巷 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:53

    There is Docker based solution called cross. All the required tools are in virtualized environment so you don't need to install additional packages for your machine. See Supported targets list.

    From project's README:

    Features

    • cross will provide all the ingredients needed for cross compilation without touching your system installation.
    • cross provides an environment, cross toolchain and cross compiled libraries, that produces the most portable binaries.
    • “cross testing”, cross can test crates for architectures other than i686 and x86_64.
    • The stable, beta and nightly channels are supported.

    Dependencies

    • rustup
    • A Linux kernel with binfmt_misc support is required for cross testing.

    One of these container engines is required. If both are installed, cross will default to docker.

    • Docker. Note that on Linux non-sudo users need to be in the docker group. Read the official post-installation steps. Requires version 1.24 or later.
    • Podman. Requires version 1.6.3 or later.

    Installation

    $ cargo install cross
    

    Usage

    cross has the exact same CLI as Cargo but as it relies on Docker you'll have to start the daemon before you can use it.

    # (ONCE PER BOOT)
    # Start the Docker daemon, if it's not already running
    $ sudo systemctl start docker
    
    # MAGIC! This Just Works
    $ cross build --target aarch64-unknown-linux-gnu
    
    # EVEN MORE MAGICAL! This also Just Works
    $ cross test --target mips64-unknown-linux-gnuabi64
    
    # Obviously, this also Just Works
    $ cross rustc --target powerpc-unknown-linux-gnu --release -- -C lto
    

提交回复
热议问题