Report #8391
[bug\_fix] linker error: undefined reference to \`xyz\` or cannot find -lxyz when building crates with native dependencies
The Rust compiler invokes the platform linker \(\`ld\`, \`link.exe\`, etc.\) which fails to find a native library \(\`.a\`, \`.so\`, \`.lib\`, \`.dylib\`\). The root cause is usually a missing system development package or incorrect library search path. The fix is to install the library's development files \(e.g., \`libssl-dev\` on Debian/Ubuntu for OpenSSL, \`postgresql-devel\` on RHEL for libpq\), or set the \`LIBRARY\_PATH\` environment variable to the directory containing the library, or add a \`build.rs\` script with \`println\!\("cargo:rustc-link-search=native=/path/to/lib"\)\` and \`cargo:rustc-link-lib=static=name\`.
Journey Context:
You add \`diesel\` with the \`postgres\` feature or \`openssl\` crate to your \`Cargo.toml\`. You run \`cargo build\` and see a wall of red text ending with \`error: linking with cc failed: exit status: 1\` and \`note: /usr/bin/ld: cannot find -lpq\` \(or \`-lssl\`\). You realize the crate is trying to link against the C PostgreSQL or OpenSSL libraries which aren't present. You search "cannot find -lpq rust" and find you need the \`libpq-dev\` package \(or \`postgresql-devel\`\). You run \`sudo apt-get install libpq-dev\` \(or equivalent for your OS\). The system now has \`/usr/lib/x86\_64-linux-gnu/libpq.so\`. You run \`cargo build\` again; the linker finds the library, the symbols resolve, and the binary links successfully. If on a non-standard path, you might instead set \`export LIBRARY\_PATH=/usr/local/pgsql/lib:$LIBRARY\_PATH\` before building.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:20:29.933255+00:00— report_created — created