`libstd` gets dynamically linked to my examples


I know that by default libstd is linked statically.
I’m building examples of this crate: https://github.com/ustulation/p2p/ and the resulting binaries have libstd linked dynamically to them:

➜  p2p git:(master) ✗ ldd target/release/examples/udp_rendezvous_server
        linux-vdso.so.1 (0x00007fff9e3a6000)
        libstd-8eeda694d27cc3ba.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3ca5743000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f3ca5e82000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f3ca553f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3ca5322000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f3ca510b000)

Now there seems not to be any special compilation flags preferring dynamic linking and RUSTFLAGS is also empty.

I’ve tried multiple rustc versions:

  • rustc 1.21.0 (3b72af97e 2017-10-09)
  • rustc 1.22.0-nightly (8493813cd 2017-10-22)
  • rustc 1.24.0-nightly (250b49205 2017-12-21)

I’ve witnessed the same behavior with all of them.

Any ideas how to debug this? :slight_smile:

Thanks in advance.


Maybe this is what you are looking for cargo rustc -- -C prefer-dynamic


I might not made myself clear, but I want to prefer static linking. Dynamic linking is what I get right now and it’s something that is unexpected.

I’ll try -C prefer-static though :wink:


Oh, I see :slight_smile:


ah, turns out rustc doesn’t have “prefer static” option:

➜  ~ rustc -C help | rg static
    -C        prefer-dynamic -- prefer dynamic linking to static linking


I can reproduce this on my machine with a fresh clone of the p2p repo, so it’s not related to your environment configuration. I don’t know why this is happening. I wonder if it is because one of the p2p dependencies? A normal “hello world” program is statically linked by default


I had the same hypothesis. I’ve tried to compile sample binary with some Tokio code and as expected libstd got linked statically.
Anyway, is there any cargo options, that would force to link libstd statically, which could be possible used by some of my dependencies?


It seems that using the net-literals crate causes it to be dynamically linked. Dunno why…


Btw, I opened an issue about this here: https://github.com/rust-lang/rust/issues/47058