cxvm: pin a GCC or Clang version per project on Linux (pyenv/nvm, but for C++ compilers)
I do embedded Linux work, and every few months a project needs a host GCC that my distro doesn't ship: usually one with the same major version as an old cross toolchain from a BSP. Building one by hand is tedious. Switching `gcc` with update-alternatives changes it for every project on the machine, which is worse.
So I wrote cxvm. It keeps as many GCC and Clang versions as you want under your home directory, and each project pins one:
```sh
cxvm install gcc@13 # GCC 13.4.0, into ~/.local/share/cxvm
cd my-project
cxvm use gcc@13 # pins gcc@13.4.0 in .cxvmrc, generates .cxvm/
cmake -S . -B build/gcc-13.4.0 -DCMAKE_TOOLCHAIN_FILE=.cxvm/toolchain.cmake
```
You commit `.cxvmrc`. Whoever clones the repo runs `cxvm install` and gets the same compiler.
Things I cared about:
- Nothing wraps the compiler. The generated toolchain file names real paths in the store, so the build still works if cxvm is off PATH or uninstalled.
- A program built with a GCC newer than the host's gets an rpath to that GCC's libstdc++. cxvm never sets LD_LIBRARY_PATH.
- Clang can use the libstdc++ of a GCC you choose: `cxvm use clang@19 --gcc gcc@13`.
- Every download is checked against a sha256 built into the binary, and a toolchain only enters the store after a smoke test. No root.
Clang comes from LLVM's official releases (11 to 18, plus every release since 19.1.0). GCC comes from xPack builds (8.5.0 and 11.2.0 to 15.2.0), or cxvm builds it from the GNU tarball. I've built 10.5.0 and 16.2.0 that way; releases back to 4.8.5 are in the catalogue but untested.
Limits: x86_64 Linux only, native compilers only, and the list of releases is compiled into cxvm, so a new GCC needs a new cxvm release. `cxvm link /opt/gcc-11` registers a compiler you already have.
It's a single static binary written in Rust. I built it with a lot of help from Claude Code; I made the design calls and tested it on real hosts myself.
Repo: https://github.com/AndreaRicchi/cxvm
It's 0.1.0, and I've only run it on Ubuntu 24.04 and 26.04. If it breaks on Fedora, Arch or anything else, I'd like to know, and also which versions or workflows you'd need next.
https://redd.it/1x42iuf
@r_cpp
Post #25840
4