Compiling 64 bit PHP on Mac OS X Leopard
My iMac ships with Leopard 10.5.2 and PHP 5.2.5 was integrated. As I was planning to build optional components into PHP, I decided to compile PHP 5.2.6 myself.
I have MySQL installed on /usr/local/mysql and to preserve the orginal settings of the preinstalled version, I used following configure options:
./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info \ --disable-dependency-tracking --with-apxs2=/usr/sbin/apxs --with-ldap=/usr \ --with-kerberos=/usr --enable-cli --with-zlib-dir=/usr --enable-trans-sid --with-xml \ --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-dbx --enable-sockets \ --with-iodbc=/usr --with-curl=/usr --with-config-file-path=/etc --sysconfdir=/private/etc \ --with-mysql-sock=/var/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-mysql=/usr --with-openssl --with-xmlrpc --with-xsl=/usr --without-pear \ --with-mysql=/usr/local/mysql
Then after building, make and install, apache failed to start as libphp5.so was in a “wrong architecture”.
Then I realized, the Apache shipped with Leopard was the 64bit version, so I have to build a 64 bit PHP.
so I added the following before configure:
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch x86_64 -g -Os -pipe" CXXFLAGS="-arch x86_64 -g -Os -pipe" LDFLAGS="-arch x86_64 -bind_at_load" export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
The last line seems redundant but strangely on my machine, the flags won’t work until I export them.
In order to configure a 64bit build, I have to install a 64bit version of MySQL, or you’ll fail the configure.
All files compiled successfully, but the linkage failed. The error was “Undefined symbol: _libiconv”. Well that’s a new one, didn’t see that when building the x86 version.
After googling for a while, I found the solution. Apple’s libiconv.dylib has a bug, it forgot to export _libiconv for both 64bit architectures (ppc64 and x86_64). The solution is to compile a universal binary libiconv yourself. (And do not forgot to install the header files too, or you’ll get other undefined symbol errors)
The flags of building universal binary is similar as the above ones. Just remember to put all four architectures (-arch i386 -arch x86_64 -arch pc7400 -arch ppc64) there.
Then finally I have the php built successfully and up n running.
Notes
To build a universal binary version of PHP, you need to build MySQL universal yourself as well, and all the libraries you used should be compiled as universal binary.
A universal binary looks like this :
$ file /usr/sbin/httpd /usr/sbin/httpd: Mach-O universal binary with 4 architectures /usr/sbin/httpd (for architecture ppc7400): Mach-O executable ppc /usr/sbin/httpd (for architecture ppc64): Mach-O 64-bit executable ppc64 /usr/sbin/httpd (for architecture i386): Mach-O executable i386 /usr/sbin/httpd (for architecture x86_64): Mach-O 64-bit executable x86_64
- Update:
After this post I’ve been using php with Leopard for a while. I found that sometimes (probably after an OS update, but I haven’t confirmed this yet) the system restores old versions of php, which would fail to load the custom-built libiconv. So you have to run “make install” & “make install-cli” again to make the new version working again. (No recompilation, only reinstall)


Igor Clark — September 1, 2008 @ 9:32 pm
Exactly what I was looking for, thanks!
Reuben Cummings — October 31, 2008 @ 6:03 am
didn’t work… but this did
http://discussions.apple.com/thread.jspa?threadID=1578979&tstart=36
It successfully loads pdo_pgsql and pgsql support. I have a MB with OSX 10.5
softboysxp — October 31, 2008 @ 11:30 pm
well when I did all this I was still using 10.5.2, cannot be sure it works on 10.5.5
Ben Chatelain — December 5, 2008 @ 7:30 am
Thanks for the info!
FYI, on Mac OS X 10.5.5 the libiconv.dylib is built as a universal binary:
$ file /usr/lib/libiconv.dylib
/usr/lib/libiconv.dylib: Mach-O universal binary with 4 architectures
/usr/lib/libiconv.dylib (for architecture ppc7400): Mach-O dynamically linked shared library ppc
/usr/lib/libiconv.dylib (for architecture ppc64): Mach-O 64-bit dynamically linked shared library ppc64
/usr/lib/libiconv.dylib (for architecture i386): Mach-O dynamically linked shared library i386
/usr/lib/libiconv.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
Despite this my PHP build was still failing with “Undefined symbols” until I found that the copy of libiconv.dylib that Fink installed was the culprit:
$ file /sw/lib/libiconv.2.4.0.dylib
/sw/lib/libiconv.2.4.0.dylib: Mach-O dynamically linked shared library i386