Rustc depending on libstdc++ makes build fail on nixos

i have successfully built rustc with ./x build --stage 1, but it fails dynamic linking.

$ ldd build/x86_64-unknown-linux-gnu/stage1/bin/rustc
	linux-vdso.so.1 (0x00007ffd38ba5000)
	librustc_driver-463fc6f98380ceb9.so => /home/binarycat/src/rs/rust/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-463fc6f98380ceb9.so (0x00007fd313a00000)
	libstd-d9ec9c88a6240622.so => /home/binarycat/src/rs/rust/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-d9ec9c88a6240622.so (0x00007fd31389a000)
	libc.so.6 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/libc.so.6 (0x00007fd3136b1000)
	libLLVM.so.18.1-rust-1.81.0-nightly => /home/binarycat/src/rs/rust/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/../lib/libLLVM.so.18.1-rust-1.81.0-nightly (0x00007fd30b600000)
	libstdc++.so.6 => not found
	libgcc_s.so.1 => /nix/store/00r99fxv6l92wk0k6wd60klhs7fkmmqj-xgcc-12.3.0-libgcc/lib/libgcc_s.so.1 (0x00007fd318813000)
	libm.so.6 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/libm.so.6 (0x00007fd3135d1000)
	/nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/ld-linux-x86-64.so.2 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib64/ld-linux-x86-64.so.2 (0x00007fd31883c000)
	librt.so.1 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/librt.so.1 (0x00007fd31880e000)
	libdl.so.2 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/libdl.so.2 (0x00007fd318809000)
	libpthread.so.0 => /nix/store/p9ysh5rk109gyjj3cn6jr54znvvlahfl-glibc-2.38-66/lib/libpthread.so.0 (0x00007fd318802000)
	libz.so.1 => /nix/store/drnjs8wypqqc0la2i4xdy38rs0c3fq2a-rust-stage0-dependencies/lib/libz.so.1 (0x00007fd3187e4000)

this seems like a simple issue of missing dependencies in a nix derivation somewhere, but i can't find any mention of the dependancies that are needed.

there is some mention of "the libstdc++ shipped with llvm", but that doesn't make sense to me, since libLLVM doesn't link to libstdc++ according to ldd

also, every source i can find says that rustc doesn't directly depend on libstdc++, so this honestly feels like a bug in the build system

related bug report

LLVM certainly does use libstdc++, but the default Rust configuration will link that statically -- you can decide whether that makes sense for you. There are further C++ wrappers for LLVM on the Rust side, but I think those are supposed to respect the static setting too.

Edit: actually the example config says that the default is [llvm] static-libstdcpp = false -- but if you use download-ci-llvm then it will use static libstdc++. I suspect you're getting a mix that way, static from the download and dynamic from Rust's local C++ bindings.

any idea how to fix it?

Probably either add libstdc++.so.6 to your nix config, or try setting [llvm] static-libstdcpp = true.

The best chance for a fix would be to link + repost this topic over at https://discourse.nixos.org/ to find someone experienced with how they set up the derivation, since it would fix the problem for everyone

i am not building rust via a nix derivation. i am building rust via x.py. this worked fine until recently, as rustc did not link to libstdc++ directly, but it does now.

getting libstdc++ from nix is trivial, the hard part is convincing the rust build system to patch rustc to use this library. stage0 automatically gets patched from .nix-deps, but stage1 does not. stage1 expects all its dependancies to be in stage1/lib, but libstdc++ does not get put there.

also:

setting static_libstdcpp is incompatible with download-ci-llvm.

1 Like

setting static_libstdcpp is incompatible with download-ci-llvm.

Hmm, that's guarded here:

but I wonder if that's missing the effect that it also has on compiler/rustc_llvm bindings. That is, you probably do want to match whatever CI has, but I don't see anything trying to figure that out.

yeah, that looks to be the problem. commenting out that line seems to do the trick.

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.