本文介绍了Ruby 'pg' gem 链接到 libpq.5.dylib 的错误副本(在 OSX 上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL 9.3 的 EnterpriseDB 安装程序将其文件放在 Mac OSX 上的 /Library/PostgreSQL/9.3/* 中.但是,Ruby gem 'pg' 从 /usr/lib 加载旧版本的 'C' 动态库 libpq.5.dylib 而不是使用正确的版本/Library/PostgreSQL/9.3/lib.尽管我已经安装了 gem,但还是会发生这种情况

The EnterpriseDB installer for PostgreSQL 9.3 places its files in /Library/PostgreSQL/9.3/* on Mac OSX. However, the Ruby gem 'pg' loads a legacy version of the 'C' dynamic library libpq.5.dylib from /usr/lib instead of using the correct version from /Library/PostgreSQL/9.3/lib. This occurs despite my having installed the gem with

gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

在需要 'pg' gem 之前,我通过设置 DYLD_PRINT_LIBRARIES 环境变量来确定加载了哪个版本的 libpq.5.dylib.

I determined which version of libpq.5.dylib is loaded by setting the DYLD_PRINT_LIBRARIES environment variable, prior to requiring the 'pg' gem.

链接到错误的动态库最明显的后果是 gem 无法使用域套接字连接到数据库,因为套接字在版本之间从 /var/pgsql_socket 重新定位到 /tmp.但是,链接到旧库也可能会导致其他问题.

The most obvious consequence of linking to the wrong dynamic library is that the gem fails to connect to the database using a domain socket, because the socket was relocated between versions from /var/pgsql_socket to /tmp. However, linking to the old library may also cause other issues.

关于如何解决此问题的任何建议?

Any suggestions on how to fix this problem?

(我正在运行 Mac OSX Lion.附加说明:对于大多数 pg 库,EnterpriseDB 安装程序将静态 (.a) 和动态 (.dylib) 版本放在 /Library/PostgreSQL/9.3/lib,但对于 libpq.5 只安装了动态版本.)

(I am running Mac OSX Lion. Additional note: For most pg libraries, the EnterpriseDB installer put both a static (.a) and a dynamic (.dylib) version in /Library/PostgreSQL/9.3/lib, but for libpq.5 installed only a dynamic version.)

推荐答案

当我在命令行运行 bin/pg_config 时,问题的原因和简单的解决方案都变得显而易见.

Both the cause of the problem and an easy solution became apparent when I ran bin/pg_config at the command line.

pg_config 生成用于控制编译和链接的变量.特别感兴趣的是 LIBDIR 和 LDFLAGS.LIBDIR 指定静态库的位置,而 LDFLAGS 提供搜索动态库的位置.在我的系统上,LIBDIR 正确设置为 /LibraryPostgreSQL/9.3/lib,但 LDFLAGS 设置如下:

pg_config generates the variables that are used to control compilation and linking. Of particular interest are LIBDIR and LDFLAGS. LIBDIR specifies the location for static libraries, while LDFLAGS provides locations to search for dynamic libraries. On my system, LIBDIR was set correctly to /LibraryPostgreSQL/9.3/lib, but LDFLAGS was set as follows:

LDFLAGS = -L../../../src/common -L/usr/local/lib -L/opt/local/20140109/lib -Wl,-dead-strip-dylibs

由于 libpq.5.dylib 不存在于任何这些位置,gem 未能找到它,而是找到了恰好安装在 /usr/的旧版本库.

Since libpq.5.dylib was not present at any of these locations, the gem failed to find it, and instead found an older version that happened to be installed at /usr/lib.

解决此问题的一种方法是将正确的文件位置注入 LDFLAGS,可能通过修改生成配置文件的 extconf.rb 中的代码.但是,在这种情况下,更简单的解决方法是在 /usr/local/lib 中将符号链接添加到正确的文件位置:

One way to fix this would be to inject the correct file location into LDFLAGS, possibly by modifying the code in extconf.rb that generates the config file. However, a much easier fix in this case is just to add a symlink in /usr/local/lib to the correct file location:

/usr/local/lib> ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib libpq.5.dylib

如果您遇到类似的问题,只需检查 pg_config 的输出,并查看是否可以在 LDFLAGS 已指定的目录之一中放置指向正确文件位置的符号链接.

If you run into a similar issue, just examine the output of pg_config, and see if you can place a symlink to the correct file location in one of the directories that is already specified by LDFLAGS.

这篇关于Ruby 'pg' gem 链接到 libpq.5.dylib 的错误副本(在 OSX 上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:28