Compile Without Linking to Speed Up Multi-Distro Builds

This is kind of a strange question and I'm not sure whether this even makes sense, but I'll probably try this out to see whether it works.

When building binaries for multiple Linux distributions with the same architecture, I have found that if I build a binary for, say, Ubuntu 22.04, it will not run on Ubuntu 20.04, even though both are amd64. Usually this is because of a different major or minor version of glibc or another system library. I have tried making static binaries to work around this in the past, but sometimes a static binary compiled on Ubuntu 20.04 will not function as expected on Ubuntu 22.04 and I've seen very weird issues in this case.

To get around this, the natural conclusion seems to be that the best solution would be to have a build matrix which builds binaries for each distribution, even though they are the same OS and architecture. When projects build RPM or DEB packages, usually this is what they have to do.

This can quickly get out of hand:

# for github actions
matrix:
 # for windows and macos, one build per architecture
 - { os: windows, arch: x86_64 }
 - { os: macos, arch: x86_64 }
 # for linux, one build per arch/distro
 - { os: ubuntu-22.04, arch: x86_64  }
 - { os: ubuntu-24.04, arch: x86_64 }
 - { os: debian-bullseye, arch: x86_64 }
 - { os: debian-bookworm, arch: x86_64 }
 # and so on and so forth...

Compiling for N different distros per architecture takes a long time to say the least.

I found this question/answer on StackOverflow which shows a way to have Cargo compile Rust code to an object file without linking it using cargo rustc -- --emit=obj.

I admittedly don't know as much about linking as I should, but could I compile Rust code on a given architecture in Linux and then copy the target/ folder to a distro-specific build environment for linking? Would this save time, or is it even possible at all?

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.