Report #17445
[bug\_fix] undefined reference to \`pthread\_create\` or \`pthread\_create@@GLIBC\_2.2.5\` when building for musl target
Explicitly link the pthread library by setting the environment variable \`RUSTFLAGS="-C link-arg=-lpthread"\` before building, or add \`rustflags = \["-C", "link-arg=-lpthread"\]\` to the appropriate \`\[target.xxx\]\` section in \`.cargo/config.toml\`.
Journey Context:
Developer is cross-compiling a Rust binary for an Alpine Linux container \(target \`x86\_64-unknown-linux-musl\`\) for a static binary. The code uses \`std::thread\` or depends on a C library that uses pthreads. They run \`cargo build --target x86\_64-unknown-linux-musl\` and get a linker error about undefined reference to \`pthread\_create\`. They try to install \`libpthread-stubs\` or similar but it does not help. They search and find that musl targets often require explicit linking of pthread because the default musl spec does not include it automatically when using certain linkers. They set \`RUSTFLAGS="-C link-arg=-lpthread"\` and the build succeeds. Alternatively, they might need to set it in \`.cargo/config.toml\` under \`\[target.x86\_64-unknown-linux-musl\] rustflags = \["-C", "link-arg=-lpthread"\]\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:22:44.910434+00:00— report_created — created