针对libpq编译postgres ruby gem.A在10.6.7



我正在尝试使用自定义编译的postgres 8.4.7安装ruby pg gem。如下图所示:

sudo env ARCHFLAGS='-arch x86_64' gem install pg -- --with-pg-config=/path/to/my/pg_config

gem会正确编译和安装,并在需要时加载正确的动态库。但是,我想静态地链接gem,以便在多台机器上实现可移植性。我最合理的尝试:

sudo env ARCHFLAGS='-arch x86_64' gem install pg -- --with-pg-config=/path/to/my/pg_config --with-ldflags='-static'

失败,显示如下错误信息:


Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb --with-pg-config=/[edited]/pgsql-8.4.7/bin/pg_config --with-ldflags=-static
checking for /[edited]/pgsql-8.4.7/bin/pg_config... yes
MacOS X build: fixing architecture flags:
  using the value in ARCHFLAGS environment variable ("-arch x86_64").
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)

任何建议吗?谢谢!

:从mkmf.log文件:


conftest.c: In function ‘t’:
conftest.c:5: error: too few arguments to function ‘PQconnectdb’
checked program was:
/* begin */
1: #include 
2: 
3: /*top*/
4: int main() { return 0; }
5: int t() { PQconnectdb(); return 0; }
/* end */

已解决:

将Postgres静态库从[prefix]/lib dir复制到一个单独的位置,并在构建gem时显式传递新路径:

sudo env ARCHFLAGS='-arch x86_64' gem install pg -- --with-pg-config=/path/to/my/pg_config --with-pg-lib=/path/to/static/libs

:

关键信息在mkmf.log文件中:

"gcc -o conftest -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0 -I.  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/[edited]/pgsql-9.0.3/include -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE  -fno-common  -pipe -fno-common   conftest.c  -L. -L/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib -static -L/[edited]/pgsql-9.0.3/lib     -lruby -lpq  -lpthread -ldl  "
ld: library not found for -lcrt0.o
collect2: ld returned 1 exit status

向链接器传递'-static'标志是试图创建一个完全静态链接的二进制文件,这在Mac OS X下是不支持的:

Mac OS X上用户二进制文件的静态链接

最新更新