Sworld

Sworld

The only way to do great work is to love what you do.

Compile Rust programs to MacOS and Linux on Win

I previously wrote an article introducing how to compile Rust programs for Linux on Windows, but the native configuration method for Rust is not very straightforward. Today, I want to introduce a simpler method to compile Rust programs for MacOS and Linux. Similarly, you can also compile for Windows on these platforms.

Cross-rs#

Cross-rs is a Rust cross-compilation tool that can compile Rust programs for other platforms with almost a single command when dependencies are not complex. Its principle is to use Docker to run the cross-compilation toolchain, so Docker or Podman needs to be installed on the machine. This library also supports testing, which is not the focus of this article, so I will just mention it here.

Installation#

It is worth noting that since this project depends on Docker, you need to ensure a stable internet connection during compilation. The download speed of Docker Hub images may be slow, so it is recommended to use an image source that you can access quickly.

With the Rust toolchain, Docker, or Podman installed, use the following command to install cross:

Basic Usage#

For simple projects, after installation, you can directly use cross to compile:

Here, I will take a Socks5 proxy software that I previously practiced writing as an example, and the compilation result is as follows:

As can be seen, it is basically divided into the following stages:

  1. Installing the toolchain for the target platform
  2. Downloading the Docker image for compilation
  3. Compiling the program

Then we get an executable file that runs on Linux, located at target/x86_64-unknown-linux-gnu/release/socks5_tcp. However, when dealing with complex projects, you may need to handle external dependencies; and when compiling for MacOS or MSVC targets, some additional configurations may be required, as described in the following sections.

Handling External Dependencies#

To customize the compilation process, we need to create a Cross.toml file in the project root directory with the following content:

The configuration method is basically as above. For further customization, please refer to the official documentation.

Compiling for MacOS#

Due to licensing reasons, the cross-rs team cannot provide us with pre-compiled images and is not allowed to distribute Apple's SDK, so we need to obtain the SDK legally ourselves to compile an image. Below, I will take aarch64-apple-darwin as an example, and other platforms are similar.

Building the Image#

We need to clone the cross-rs code, update the submodules, and build the image after configuration:

The SDK version requirements for building are as follows:

  • i686-apple-darwin: SDK <= 10.13
  • x86_64-apple-darwin: SDK <= 13.0 or SDK <= 12.4
  • aarch64-apple-darwin: SDK >= 10.16 and (SDK <= 13.0 or SDK <= 12.4)

Configuring Cross.toml#

After the build is complete, it becomes very simple. We just need to create a Cross.toml file in the project root directory, specifying that the corresponding target uses our local image, with the following content:

Subsequent compilations will be the same as for Linux, and I will not elaborate further.

Compiling for Windows#

MSVC may be the optimal target for Windows, but for convenience, I still chose the GNU toolchain. We can use x86_64-pc-windows-gnu or i686-pc-windows-gnu as targets, and the compilation method is similar to Linux:

This way, we can compile executable files for Windows on Linux or MacOS without needing to configure MSVC. If you really want the MSVC compilation results, please read the README and Issues of the official repository.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.