Compile postgres ruby gem against libpq.a on 10.6.7
I'm trying to install the ruby pg gem using a custom compiled postgres 8.4.7. This works as expected:
sudo env ARCHFLAGS='-arch x86_64' gem install pg -- --with-pg-config=/path/to/my/pg_config
The gem compiles and installs correctly, and when required it loads the right dynamic library. However, I'd like to statically link the gem for portability on multiple machines. My most reasonable attempt:
sudo env ARCHFLAGS='-arch x86_64' gem install pg -- --with-pg-config=/path/to/my/pg_config --with-ldflags='-static'
fails with this error message:
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/开发者_JAVA技巧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)
Any suggestion? Thanks!
Update: From the mkmf.log file:
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 */
Solved:
Copy Postgres static libraries to a separate location from the [prefix]/lib dir and pass the new path explicitly when building the 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
Details:
The crucial information was a bit up in the mkmf.log file:
"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
Passing the '-static' flag to the linker was trying to create a completely statically linked binary, which is not supported under Mac OS X:
Static linking of user binaries on Mac OS X
精彩评论