【问题标题】:ld: library not found for -lpq when build rust in macOSld:在 macOS 中构建 rust 时找不到 -lpq 的库
【发布时间】:2022-06-11 23:59:07
【问题描述】:

当我使用以下命令在 macOS 中使用 apple silicon 构建我的 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


【解决方案1】:

首先,我运行这些命令:

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

【讨论】:

    【解决方案2】:

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

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

    Cargo.toml

    这是我的Cargo.toml 文件供版本参考:

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

    柴油机命令行界面

    这是我的 Diesel CLI 版本的输出

    < diesel --version
    > diesel 1.4.1
    

    【讨论】:

    • 谢谢!它解决了我在蒙特雷的问题
    【解决方案3】:

    我可以解决:

    cargo clean
    

    稍后

    cargo build
    

    【讨论】:

      【解决方案4】:

      似乎在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 2021-08-24
        • 2019-01-25
        • 2015-02-05
        • 2021-06-07
        • 2018-06-07
        • 1970-01-01
        相关资源
        最近更新 更多