简体   繁体   English

如何修复 Rust 柴油 cli 链接 libpq.lib 安装错误

[英]How to fix Rust diesel cli link libpq.lib error on install

I'm trying (for hours now) to install the cargo crate diesel_cli for postgres.我正在尝试(现在几个小时)为 postgres 安装货物板条箱diesel_cli However, every time I run the recommended cargo command:但是,每次我运行推荐的 cargo 命令时:

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

I wait a few minutes just to see the same build fail -> abort error, with this message:我等了几分钟才看到同样的构建失败 -> 中止错误,并显示以下消息:

note: LINK : fatal error LNK1181: cannot open input file 'libpq.lib'


error: aborting due to previous error

error: failed to compile `diesel_cli v1.4.1`, intermediate artifacts can be found at `C:\Users\<user name here>\AppData\Local\Temp\cargo-installUU2DtT`

Caused by:
  could not compile `diesel_cli`.

I'm running postgres in a docker container and have the binaries on my C:\pgsql with the lib and bin directories both on the PATH so I can't figure out why it's not linking.我在 docker 容器中运行 postgres,并在我的C:\pgsql上有二进制文件,其中libbin目录都在PATH上,所以我不知道为什么它没有链接。 What else could be required they didn't mention in the docs?他们在文档中没有提到的其他要求是什么?

In my case the installation was successful but when I tried to run it this error occured.在我的情况下,安装是成功的,但是当我尝试运行它时发生了这个错误。 maybe this would work for others who have the same problem:也许这适用于其他有同样问题的人:

  • open PowerShell打开 PowerShell
  • type in setx PQ_LIB_DIR "C:\Program Files\PostgreSQL\13\lib" (or any other path to your PostgreSQL lib)输入setx PQ_LIB_DIR "C:\Program Files\PostgreSQL\13\lib" (或 PostgreSQL 库的任何其他路径)
  • restart your PC重启你的电脑
  • run again再次运行

I had the same issue with WSL, if you're on Linux probably you could find PostgreSQL lib location and add it to your environment variables.我在 WSL 上遇到了同样的问题,如果您使用的是 Linux,您可能会找到 PostgreSQL 库位置并将其添加到您的环境变量中。

Adding the folder to the PATH variable didn't help, at least in my case, as by some reason it is not used in the /LIBPATH parameter passed to link.exe .至少在我的情况下,将文件夹添加到PATH变量没有帮助,因为由于某种原因,它没有在传递给link.exe/LIBPATH参数中使用。 In my case it was C:\Users\<username>\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib You can see it in the beginning of the error message.就我而言,它是C:\Users\<username>\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib你可以在开头看到它错误信息。 Copy libpq.lib in there and it will be used from there.libpq.lib复制到那里,它将从那里使用。

After installation diesel would require some other assemblies.安装柴油后,需要一些其他组件。 Copy libcrypto-1_1-x64.dll , libiconv-2.dll and libssl-1_1-x64.dll into the folder showed after where diesel command executionlibcrypto-1_1-x64.dlllibiconv-2.dlllibssl-1_1-x64.dll到执行where diesel命令后的文件夹中

I had the same error on Ubuntu and for me the following install fixed the issue:我在 Ubuntu 上遇到了同样的错误,对我来说,以下安装解决了这个问题:

sudo apt install libpq-dev

To give clear steps for windows:为 windows 提供明确的步骤:

  1. Add C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib in the path in environment variables在环境变量的路径中添加C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib

  2. Copy libpq.lib that is in C:\Program Files\PostgreSQL\14\lib (obviously this is with version 14) and paste it in C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib复制 C:\Program Files\PostgreSQL\14\lib 中的 libpq.lib(显然这是版本 14)并将其粘贴到 C:\Users<username>.rustup\toolchains\stable-x86_64-pc msvc\lib\rustlib\x86_64-pc-windows-msvc\lib

No need to move files around, just add C:\Program Files\PostgreSQL\14\lib and C:\Program Files\PostgreSQL\14\bin to your PATH.无需移动文件,只需将C:\Program Files\PostgreSQL\14\libC:\Program Files\PostgreSQL\14\bin添加到您的 PATH 中。 Installing and running diesel should have no problems.安装和运行柴油应该没有问题。

Note: your paths may be different, and remember to close/reopen your terminal so the PATH variable is updated.注意:您的路径可能不同,请记住关闭/重新打开终端,以便更新 PATH 变量。

(Tested on Windows 10) (在 Windows 10 上测试)

If you've attempted to cargo build (or anything that runs the build scripts for libpq rust crate) when your environment was invalid, then you need to do a cargo clean after fixing your environment otherwise you'll still get the libpq.lib not found error even when it's in your PATH.如果您在环境无效时尝试进行货物构建(或运行 libpq rust 板条箱的构建脚本的任何东西),那么您需要在修复环境后进行cargo clean ,否则您仍然会得到 libpq.lib即使它在您的 PATH 中也发现错误。 The other answers where you copy the file into another directory are just hacks将文件复制到另一个目录的其他答案只是黑客

You can instead add the path to .../lib to the compilers' library search paths, using RUSTFLAGS environment variable .您可以改为使用RUSTFLAGS环境变量.../lib的路径添加到编译器的库搜索路径。 It works both for installing diesel_cli and for building your projects.它适用于安装diesel_cli和构建您的项目。

RUSTFLAGS='-L /usr/local/pgsql/lib' cargo build

On Windows with EDB installer, the path contains a space, so use CARGO_ENCODED_RUSTFLAGS instead.在带有 EDB 安装程序的 Windows 上,路径包含一个空格,因此请改用CARGO_ENCODED_RUSTFLAGS For PowerShell:对于 PowerShell:

$env:CARGO_ENCODED_RUSTFLAGS = "-L`u{1f}C:\Program Files\PostgreSQL\14\lib"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM