18

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"
Dolphin
  • 29,069
  • 61
  • 260
  • 539

5 Answers5

26

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

Arco
  • 614
  • 5
  • 13
12

I solved it with:

cargo clean

later

cargo build
josedlujan
  • 5,357
  • 2
  • 27
  • 49
11

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
Esteban Borai
  • 2,311
  • 1
  • 21
  • 27
5

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
hernvnc
  • 827
  • 11
  • 18
1

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

Dolphin
  • 29,069
  • 61
  • 260
  • 539