Where to get cross-linkers and cross-libs?

Okey, so as far as I understood Rust compiler can cross-compile code, but it requires me to provide cross-linker and cross-libc to be able to do it, is this statement correct? I'm on Windows operating system at work and I want to cross-compile from Windows to Linux and Mac OSX. How can I do it? Where do I get thoose cross-linkers and cross-libcs?

1 Like

I believe you want to look into Xargo: GitHub - japaric/xargo: The sysroot manager that lets you build and customize `std`

I have experimented (somewhat successfully) with the reverse: developing Rust on Linux, and getting it to compile into a Windows DLL.

Fortunately, Mac OS X, Linux, and Windows are all tier 1 targets and thus have prebuilt versions of the Rust standard library, so the process should be more straightforward and not require xargo.

First, download the Rust standard library for the target. This is most easily done with the "rustup" tool:

rustup target add x86_64-unknown-linux-gnu --toolchain stable

This will allow the compiler to attempt a syntax check (and find all its dependencies) with the check comand in cargo:

cargo check --target x86_64-unknown-linux-gnu

But that is the easy part. If you try to run a build, you will find the Rust compiler trying to invoke an external linker, because it does not use the one that ships with LLVM on most platforms. You will need a program that can link the binary/shared library format for the target.

On Linux, my distro ships a pre-built GNU cross compiler for Windows, and that includes a cross-linker -- that is, a Linux executable which knows how to link DLLs and EXEs with "API shims" to represent the standard Windows DLLs (such as kernel32, the kernel API, and msvc32, the C runtime).

How to accomplish the reverse, I'm afraid, is beyond my expertise. A long time ago, I once managed to compile an A-B-C cross compiler (sometimes called a "Canadian" cross) in Cygwin from source. That is, I used the Cygwin version of GCC to build a GCC binary that ran on Win32 without Cygwin (courtesy of MinGW) that created MIPS binaries without a libc when executed. (This was for embedded development.) However, I'm afraid I cannot offer any advice from that... experience. :smile:

Suffice it to say: the Rust toolchain generally knows what to do if you give it a nudge or two, but linking is something that's per-platform. I'm afraid someone deeper in Windows development will have to help you from there.

Hope this helps.

EDIT: spelling and grammar

ANOTHER EDIT: Upon further thought, probably the easiest way is to install MinGW and then use its GCC to build binutils in a cross configuration. After you install MinGW and GCC, something like this guide should do it, except your target would be "x86_64-unknown-linux-gnu"

I remember reading that nightly now uses a bundled version of LLD, the LLVM linker, and I'm pretty sure would be able to do cross-linking by default. It's not stable (or even guaranteed to work) though, so your mileage may vary.

1 Like

Even if LLD gets integrated I would still need prebuilt platform specific libraries.