ld:在 macOS 中构建 rust 时找不到 -lpq 的库

问题描述 投票:0回答:6

当我使用以下命令在 macOS 中使用 apple sillicon 构建 rust 项目时:

CARGO_HTTP_MULTIPLEXING=false cargo build

显示如下错误:

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

我已尝试安装

brew install libpq
brew link --force libpq

仍然没有解决这个问题,我该怎么做才能解决这个问题?是不是 PostgreSQL 库现在不支持 Apple Silicon(Apple M1 Pro)?这是我的项目依赖项:

[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"
rust linker-errors
6个回答
34
投票

首先,我运行这些命令:

brew install libpq
brew link --force libpq

然后:

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

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

它对我有用。

macOS:大苏尔 11.5


18
投票

我解决了这个问题:

cargo clean

稍后

cargo build

13
投票

我在我的项目中使用 Diesel ORM:https://github.com/EstebanBorai/estebanborai.com 这就是我的设置方式 支持 Diesel 的项目。

  1. 使用 Homebrew 安装
    libpq
brew install libpq && brew link --force libpq
  1. 然后将库添加到您的路径中
echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
  1. 最后安装 Diesel CLI
cargo install diesel_cli --no-default-features --features postgres

货物.toml

这是我的

Cargo.toml
文件,供版本参考:

# -- snip --

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

# -- snip --

柴油 CLI

这是我的 Diesel CLI 版本的输出

< diesel --version
> diesel 1.4.1

5
投票

BigSur 和/或蒙特雷的路径似乎已移动。我通过显式指定库路径解决了这个问题。

IE 在 CLI 中运行 rustc:

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

您也可以在 RUSTFLAGS 环境变量中指定它,IE:

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

然后运行:

rustc ./main.rs

或:

cargo build

在您的示例中,您可以这样做:

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

1
投票

我在运行我的货物构建时遇到了此错误消息的问题:

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

我解决这个问题的方法是使用

brew

安装所需的 postgres C 库
brew install libpq
brew link --force libpq

如果您像我一样使用

fish
,我还添加了一个如下链接:
fish_add_path /opt/homebrew/opt/libpq/bin


0
投票

在索诺玛

brew install libpq && brew link --force libpq
cargo clean
cargo install diesel_cli --no-default-features --features postgres
© www.soinside.com 2019 - 2024. All rights reserved.