Rust Cross compilation - the right 'rustc' compiler on the host

Hello,

Being newbie to rust world, trying to understand rust cross compilation traits.

On x86_64 (os:linux) build machine, trying to compile a sample help app for the target aarch64.

Have installed the target : rustup target add aarch64-unknown-linux-musl.

Now able to cross compile the hello app too: cargo build --target aarch64-unknown-linux-musl. Linker is also set to target appropriately and binary successfully runs on the target machine (arm64).

When I looked at 'strace' o/p, I see that, the compiler 'rustc' is being used from the host machine, host architecture : /home/vmohare/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc

Is this the eight compiler to be used in this case? or should I be installing and using from stable-x86_64-unknown-linux-gnu/bin/rustc?

Does your code or any of its transitive dependencies include procedural macros or build scripts? Because those need to be compiled for the host, not for the target.

In this case, there's nothing you need to change.

1 Like

What is happening should be correct.

rustc is a "native" cross compiler. This means that every rustc is capable of generating code for any supported target. So a rustc compiled for x86-64 Linux is being instructed to generate code for aarch64 Linux. This is normal.

The rustup target add part installs the necessary support files for that target. This does not include the compiler, because you already have one. It can include things like libraries, or support programs.

2 Likes

Notably, this is the reason that rustc does not accept any instruction set or target that requires licenses to generate code.

Thank you for clarifying.
While lots of material available explaining how to cross compile, possibly, this aspect is missing.
Would be good to document (if not done already) the fact that ' rustc is a "native" cross compiler' would be very helpful for many.

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.