Report #16882
[bug\_fix] ld: library not found for -l \(e.g., -lssl, -lpq\) during cargo build
Install the missing system library \(e.g., \`brew install openssl\`, \`apt-get install libssl-dev\`\), then set the environment variable \`PKG\_CONFIG\_PATH\` to point to the library’s \`pkgconfig\` directory \(e.g., \`export PKG\_CONFIG\_PATH="/opt/homebrew/opt/openssl/lib/pkgconfig"\`\), or set the crate-specific override variable \(e.g., \`OPENSSL\_DIR\`\) documented by the \`-sys\` crate to the library root. This provides the linker with the search path for the native \`.a\` or \`.dylib\`/\`.so\` files.
Journey Context:
The developer adds \`reqwest\` with \`native-tls\` or \`diesel\` with \`postgres\` to their \`Cargo.toml\`. Upon \`cargo build\`, the build fails at the final linking stage with a message like \`ld: library not found for -lssl\` on macOS or \`collect2: error: ld returned 1 exit status\` mentioning \`-lpq\`. The developer installs OpenSSL via \`brew install openssl\`, but the error persists. They search StackOverflow and find advice to run \`brew link --force openssl\`, which is dangerous and often fails on macOS with permission errors. They try to set \`LIBRARY\_PATH\` to \`/usr/local/lib\`, not realizing Homebrew on Apple Silicon uses \`/opt/homebrew/lib\`. They attempt to modify the \`build.rs\` of the crate itself. Finally, they read the documentation for \`openssl-sys\` or \`libpq-sys\` and discover that these crates respect environment variables like \`OPENSSL\_DIR\` or \`PKG\_CONFIG\_PATH\`. They set \`export PKG\_CONFIG\_PATH="/opt/homebrew/opt/openssl/lib/pkgconfig"\` and \`export OPENSSL\_DIR="/opt/homebrew/opt/openssl"\`, and the build succeeds. The journey highlights the opaque boundary between Cargo’s high-level build and the system linker, and the realization that \`-sys\` crates are thin wrappers around \`pkg-config\` or manual path overrides.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:52:45.059464+00:00— report_created — created