Building a cross-platform application (using Rust)

Yes, you won't need to change the source, unless you are using specific libraries that are not cross-platform.

But as @dbaupp said native executables are different on each platform, *nix uses ELF, Windows PE, and OSX Mach-O. So you will need to compile it for each platform.

I don't know the state of cross-compiling in rust, but if they already implemented it, then you should be able to build all the binaries in the same platform, if not, you will have to build each binary on it's platform.


To run on multiple platforms you need to build an executable for each as @huon-dbauapp commented.

This is fairly straightforward with Rust. You use "--target=" with rustc to tell it what you want to build. The same flag works with Cargo.

For example, this builds for an ARM target:

cargo build --target=arm-unknown-linux-gnueabihf

See the Rust Flexible Target Specification for more about targets.

However, Rust doesn't ship with the std Crate compiled for ARM (as of June 2015). If this is the case for your target, you'll first need to compile the std Crates for the target yourself, which involves compiling the Rust compiler from source, and specifying the target for that build!

For information, most of this is copied from: https://github.com/japaric/ruststrap/blob/master/1-how-to-cross-compile.md

The following instructions are for gcc, so if you don't have this you'll need to install it. You'll also need the corresponding cross compiler tools, so for gcc:

sudo apt-get install gcc-arm-linux-gnueabihf

Compile Rust std Crate For ARM

The following example assumes you've already installed the current Rust Nightly, so we'll just get the sources and compile for ARM. If you are using a different version of the compiler, you'll need to get that to ensure your ARM libraries match the version of the compiler you're using to build your projects.

mkdir ~/toolchains
cd ~/toolchains
git clone https://github.com/rust-lang/rust.git
cd rust
git update

Build rustc for ARM

cd ~/toolchains/rust
./configure --target=arm-unknown-linux-gnueabihf,x86_64-unknown-linux-gnu
make -j4
sudo make install

Note "-j4" needs at least 8GB RAM, so if you hit a problem above try "make" instead.

Install ARM rustc libraries In native rustc build

sudo ln -s $HOME/src/rust/arm-unknown-linux-gnueabihf /usr/lib/rustlib/arm-unknown-linux-gnueabihf

Create hello.rs containing:

pub fn main() {
    println!("Hello, world!");
}

Compile hello.rs, and tell rustc the name of the cross-compiler (which must be in your PATH):

rustc -C linker=arm-linux-gnueabihf-gcc-4.9 --target=arm-unknown-linux-gnueabihf hello.rs

Check that the produced binary is really an ARM binary: $ file hello hello: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), (..)

SUCCESS!!!:

Check: the binary should work on an ARM device

$ scp hello me@arm:~
$ ssh me@arm ./hello

Hello, world!

I've used this to build and link a Rust project with a separate C library as well. Instructions similar to the above on how to do this, dynamically or statically are in a separate post, but I've used my link quota up already!


It looks like the rust compiler might not be ready to build standalone binaries for windows yet (see the windows section here), so this probably can't be done yet.

For posix systems it should mostly Just Work unless you're trying to do GUI stuff.


The best way to figure this out is to download the source code for Servo and explore it on your own. Servo is absolutely a cross-platform codebase, so it will have to address all of these questions, whether they be answered in build/configuration files, or the Rust source itself.