Dolphin
Dolphin

Reputation: 39135

ld: library not found for -lpq when build rust in macOS

When I build my rust project in macOS with apple sillicon using this command:

CARGO_HTTP_MULTIPLEXING=false cargo build

shows error like this:

  = note: ld: library not found for -lpq
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have tried to install

brew install libpq
brew link --force libpq

still did not fix this problem, what should I do to fix this problem? Is it the PostgreSQL lib did not support Apple Sillicon(Apple M1 Pro) right now? This is my project dependencies:

[package]
name = "reddwarf_dict"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
# database
diesel = { version = "1.4.7", features = ["postgres"] }
dotenv = "0.15.0"
chrono = "0.4"
log = "0.4"
env_logger = "0.9.0"
config = "0.11"
rust_wheel = "0.1.0"

Upvotes: 26

Views: 12099

Answers (6)

DrGo
DrGo

Reputation: 541

On Sonoma

brew install libpq && brew link --force libpq
cargo clean
cargo install diesel_cli --no-default-features --features postgres

Upvotes: 0

josedlujan
josedlujan

Reputation: 5600

I solved it with:

cargo clean

later

cargo build

Upvotes: 24

Steven Kotwal
Steven Kotwal

Reputation: 71

I ran into this problem for this error message when running my cargo build:

= note: ld: library not found for -lpq
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

The way I solved it was by installing the required postgres C libraries using brew

brew install libpq
brew link --force libpq

Optionally if you are using fish like I was I also added a link like this: fish_add_path /opt/homebrew/opt/libpq/bin

Upvotes: 2

Leo Borai
Leo Borai

Reputation: 2509

I'm using Diesel ORM in my project here: https://github.com/EstebanBorai/estebanborai.com and this is how I set up the project to support Diesel.

  1. Install libpq using Homebrew
brew install libpq && brew link --force libpq
  1. Then add the library to your PATH
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
  1. Finally install Diesel CLI
cargo install diesel_cli --no-default-features --features postgres

Cargo.toml

This is my Cargo.toml file for reference on versions:

# -- snip --

[dependencies]
diesel = { version = "1.4.4", features = ["chrono", "postgres", "r2d2", "uuidv07"] }
r2d2 = "0.8.9"

# -- snip --

Diesel CLI

This is the output for my Diesel CLI version

< diesel --version
> diesel 1.4.1

Upvotes: 16

hernvnc
hernvnc

Reputation: 1035

It seems paths got moved in BigSur and/or Monterrey. I solved it by explicitly specifying the library path.

IE running rustc in the CLI:

rustc -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib ./main.rs

You can also specify it in the RUSTFLAGS env var, IE:

export RUSTFLAGS='-L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'

And then run:

rustc ./main.rs

or:

cargo build

In your example, you could do:

CARGO_HTTP_MULTIPLEXING=false RUSTFLAGS='-L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'
 cargo build

Upvotes: 5

Arco
Arco

Reputation: 964

Firstly, I run these commands:

brew install libpq
brew link --force libpq

Then:

PQ_LIB_DIR="$(brew --prefix libpq)/lib"

cargo install diesel_cli --no-default-features --features postgres

It works for me.

macOS: Big Sur 11.5

Upvotes: 40

Related Questions