After thinking a lot about it, I started porting a little software I made initially in Go, to Rust. Let’s say I started having real issues with the language, the decisions that were made, and the general behavior "embrace it or get out, but don’t bother us ". So, Rust.
The development is going quite well, pretty sure the quality of the code is not that good, but I’m learning a lot, I love the design choices that were made (sum types feel like a blessing, clap puts any CLI parser to shame), however I’m worried about the compilation for multiple platforms, which was really not a worry in Go.
My question can be stated as follows : having a Linux machine and only that, how can I generate executables for Linux, Mac and Windows so that they just have to run it and that’s it (it’s a CLI program). If I can avoid having them install external dependencies, that would definitely be a plus. My only dependencies are Clap and Random. Also, I will not be able to test them, is there any open-source platform that would allow me to test an executable on multiple OS ?
Thanks a lot.
Cross compilation is already built into the Rust compiler by default, a lot of the time it’s as simple as writing
cargo build --target i686-pc-windows-msvc. By default you’ll normally only have a copy of the standard library for your host platform, the
rustup README has a section which explains how you can access pre-compiled artefacts for other platforms as well.
Things can sometimes get a bit tricky when one of your dependencies links to a native (i.e. non-Rust) library, because you’ll need to make sure that cross-compiles as well. In these cases I’ll typically use the cross tool. It’s essentially a wrapper around
cargo which does all cross compilation in
docker images that have all the necessary bits and pieces installed.
Don’t forget that Rust by itself doesn’t know how to make executables nor dynamic libraries.
Rust is dependent on having target system’s linker, so for executables and dynamic libraries
cargo build --target= only prints ugly errors unless you also:
- install appropriate linker for the platform
- tell Cargo where to find the linker
- almost always you’ll also need target system’s C library and standard libraries (e.g. a bunch of Windows DLLs and Visual Studio runtime for msvc target).