GCC 静态联编的最简便方法
今天SUNU想要编译一个PHP的静态链接版本(不依赖任何动态库,包括GLIBC), 修改LDFLAGS, CFLAGS 无果,修改Makefile太耗时耗力,也不见得管用,还得去熟悉PHP的编译环境。
要想静态编译,无非就是要将-static选项传递给GCC,至于GCC是将这个选项传给编译器还是传给LD链接器,俺不管。GCC的MANPAGE讲得很清楚:
-static
On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.
于是,想出一个狠招, 把GCC 移花接木一下:
mv /usr/bin/gcc /usr/bin/gcc-old
cat >/usr/bin/gcc <<EOF
#/bin/bash
gcc-old -static "$@"
EOF
chmod +x /usr/bin/gcc
接着按照正常步骤在PHP源码目录里 ./configure && make即可
make 完以后,记得将正常的GCC恢复回来。
PHP的编译环境算比较复杂的,这个方法针对其它源码的静态联编应该同样适用。让LDFALAG, CFLAG 见鬼去吧 。