How to build for all operating systems?

I made a small cli in rust, and now I want to distribute it, but then I had a problem, when I run cargo build -r in linux, the result is not compatible with Windows for example. so what I want is to have a built version that runs on linux, another that runs on windows... that is, one for each operating system, so I just need to distribute it. how do I do that?

You can pass the --target <target-triple> flag to cargo build in order to cross-compile. (You have to first install any targets you want to use in this way via rustup target add <target-triple>.)

No, this doesn't work.

I'm really annoyed when the cross-compilation problem is presented in such trivial way, because this just results in a bunch of errors when compiling any real-world project.

Rust doesn't ship with a linker, libc, C compiler, or any system libraries. The --target ability is nice, but it's only one piece of a puzzle that Rust doesn't solve.

My personal advice would be to use Rust's cross-compilation ability only for targeting different Linux architectures. It's usable for something like x86 Linux compiling for ARM Linux, and only if you also install a bunch of system-specific Linux packages for cross-compilation.

But for anything else, don't bother. Get a Windows PC or a Windows VM and compile there natively.

There are also prepackaged VMs for various targets:


The easiest way to do this is in CI (e.g. by wiring up GitHub Actions to your repo). Most CI providers will let you do something when a tagged commit is pushed to the repo, and from there you can run the same release job on Windows, MacOS, and Linux.

How you actually implement this will depend on how you want to release your stuff, but the general flow is:

  • When a v* tag is pushed up
  • First, create a new draft release in GitHub Releases
  • For Windows, Linux, and MacOS, compile with --release and attach the binary to the release
  • When the binaries have been compiled and uploaded, mark the release as published

I wouldn't mess around with cross or trying to cross-compile to each target OS yourself. Windows and MacOS are next to impossible to cross-compile to in practice (e.g. Apple doesn't let you legally distribute MacOS system libraries, so you can only do mac builds if you have a mac).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.