Compile a binary for a specific platform

Hi all,

I didn't find in the cargo documentation (Cargo Targets - The Cargo Book) a way to specify a [[bin]] target for a specific platform only (e.g.: Windows). Is it possible ?

Thanks for your help.

No, there's not any built-in support for this. There are some hacky approaches, but they have their disadvantages:

  • You can use required-features, but then you need to manually pass different feature arguments when building for different platforms.

  • You can use #[cfg(...)] inside the binary to make it compile just a stub when building for other platforms, but this still outputs a binary on every platform.

  • You could move the binary to a separate Cargo package, and build that package only for the supported platform.

@mbrubeck Thanks for your hints. I'll probably use your second tip.
@her0mx Actually it's a binary not available for Windows because using UNIX domain sockets, and I use this binary for my integration tests. So I don't want to compile it for Windows, but for other Unices.

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.