Mac M1上无法链接libpq

8

我一直在尝试在我的 Macbook M1 上运行 Rust Diesel crate,但是它无法正常工作。编译的最后一部分会被以下错误打断:

  = note: ld: warning: ignoring file /usr/local/Cellar/libpq/14.1/lib/libpq.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
          Undefined symbols for architecture arm64:

当我获取 libpq 的信息时,我得到以下结果:
maxwellflitton@Maxwells-MacBook-Pro vanguard % brew info libpq                                                           
libpq: stable 14.1 (bottled) [keg-only]
Postgres C API library
https://www.postgresql.org/docs/14/libpq.html
/usr/local/Cellar/libpq/14.1 (2,335 files, 27.8MB)
  Poured from bottle on 2022-01-09 at 00:14:32
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libpq.rb
License: PostgreSQL
==> Dependencies
Required: krb5 ✔, openssl@1.1 ✔
==> Caveats
libpq is keg-only, which means it was not symlinked into /usr/local,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc

For compilers to find libpq you may need to set:
  export LDFLAGS="-L/usr/local/opt/libpq/lib"
  export CPPFLAGS="-I/usr/local/opt/libpq/include"


我尝试使用以下命令进行安装:
RUSTFLAGS='-L /usr/local/Cellar/libpq/14.1/lib' cargo install diesel_cli --no-default-features --features postgres --force

但我还是得到了同样的错误。如果这样做更容易,是否只需清除整个内容并重新开始,如果是,我该如何做呢?其他使用M1的互联网上的人似乎可以通过一个简单的brew install libpq来解决问题。我以前的英特尔mac从未遇到过任何问题。我的~/.cargo/config.toml具有以下配置:

[target.aarch64-apple-darwin]
rustflags = '-L /usr/local/Cellar/libpq/14.1/lib -L /opt/homebrew/lib'

这个回答解决了你的问题吗?尝试在Mac air m1上安装diesel时出现问题 - weiznich
嗨Max,我现在正在阅读你的书,看到你的名字出现在这篇文章中很有趣。我和你遇到了同样的问题。这个问题解决了吗?当我尝试安装柴油时,我最终得到了这个消息:“ld: warning: ignoring file /usr/local/Cellar/libpq/14.5/lib/libpq.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64”。 - Mactov
@Mactov 嘿,抱歉已经过去一年了,我最终彻底清除了我的MacBook,重新开始了一切,看起来似乎起效了。 - undefined
5个回答

8

惊人地,这起作用了

  • brew install postgresql libpq(安装postgresql和libpq)
  • cargo clean(清理依赖)
  • cargo build(构建)
  • cargo install diesel_cli --no-default-features --features postgres(安装diesel_cli并指定postgres为特性)

我认为这可能与在安装必要的依赖项之前安装diesel-cli有关。清理cargo依赖并重新安装对我起作用。


1
谢谢,清理货物对我来说是缺失的步骤。可能与一个无效的位置有关。 - Joe Meyer

4

我遇到了类似的问题,问题源于我的brew配置(我从我的Intel Mac恢复了文件系统):

❯ brew config
...
macOS: 12.6-x86_64
...

因此,Brew 正在下载适用于 Intel 处理器的软件包。我按照 官网 的指示卸载并重新安装了 Brew ,现在 brew config 已经正确:
❯ brew config
...
macOS: 12.6-arm64
...

希望这能帮到你!

2

在我的 M1 Mac 上,我使用 brew 安装了 PostgreSQL 客户端和库:

brew install postgresql libpq

我尝试再次安装diesel_cli,这次成功了:

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

出于某些原因,仅仅安装libpq是不足以安装diesel_cli的,我必须同时安装Postgres库和客户端。

我能够构建diesel,但是当我尝试运行它时仍然会出现错误。 在22.52秒内完成了[优化]目标的发布 正在替换 /Users/a/.cargo/bin/diesel 用 `diesel_cli v1.4.1` (可执行文件 `diesel`) 替换了软件包 `diesel_cli v1.4.1` ➜ git:(master) ✗ diesel setup dyld[72305]: 找不到符号在平面命名空间 '_PQclear' [2] 72305 abort diesel setup - Anant

1

我有一台Mac M1,并使用brew安装了postgresql和libpq。

最终,我在我的~/.cargo/config.toml文件中使用以下行成功安装了diesel_cli。

[target.aarch64-apple-darwin]
rustflags = '-L /opt/homebrew/opt/libpq/lib -L /opt/homebrew/lib'

源代码在https://github.com/diesel-rs/diesel/issues/2605处。


这是我在尝试在 Mac M2 Max 上本地构建 Lemmy(lemmy_server crate)时解决错误的方法。 - Sparrow1029

0

我正在尝试编译一个我建立的 Rust 项目。这是因为目标是尝试编译为 arm64,然而它使用的 postgres 的 dylib 是为 x64_86 编译的。所以它会抛出这个错误。

我尝试重新安装 libpq,但仍然有相同的错误 - 可能将其编译为 x64_86 并通过 rosetta 运行。

这是我遇到的确切错误:

Undefined symbols for architecture arm64:
            "_PQclear", referenced from:

ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

最终我重新安装了 postgresqllibpq,并使用 arm64 变体,同时保留了 x64_86 以供那些没有更新且需要使用 x64_86 代码的项目使用:

https://codetinkering.com/switch-homebrew-arm-x86/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接