<HEAD> php
下面的操做是在Ubuntu 12.04下,而且已经搭建了LAMP环境.
git
</HEAD> github
一.下载PHP源码 sql
1.首先安装GIT
apache
sudo apt-get install git
2.克隆PHP源码 vim
cd / git clone https://github.com/php/php-src.git ls会看到php-src文件夹
3.进入ext目录 curl
cd php-src/ext ls会看见不少扩展如curl,pdo等,同时还会看见用来创建扩展的脚本ext_skel
二.创建骨架修改参数 函数
1.利用ext_skel创建骨架 测试
./ext_skel --extname=yourname
yourname为你想创建的扩展的名字,咱们先建一个,例如为rube 网站
创建好后当前文件夹下会出现rube这个文件夹
cd rube2.修改config,m4的参数
vim config.m4 dnl Otherwise use enable: PHP_ARG_ENABLE(rube, whether to enable rube support, dnl Make sure that the comment is aligned: [ --enable-rube Enable rube support])
将PHP_ARG_ENABLE(rube, whether to enable rube support 和 [ --enable-rube Enable rube support] 这两行前面的dnl 去掉 。修改为如上所示
三.编写php_rube.h 和 rube.c
1.编辑php_rube.h
vim php_rube.h
在php_rube.h的最后面添加
PHP_FUNCTION(confirm_rube_compiled); PHP_FUNCTION(hello);hello 为你要建立的那个函数
2.编辑rube.c
vim rube.c const zend_function_entry rube_functions[] = { PHP_FE(confirm_rube_compiled, NULL) PHP_FE(hello, NULL) PHP_FE_END };修改zend_function_entry rube_functions[] , 在PHP_FE(confirm_rube_compiled, NULL)后面添加
PHP_FE(hello, NULL)
3.编写函数
接下来编写hello这个函数,首先编写一个简单的输出"Hello my first extention"的函数。。。
在rube.c的最后面添加
PHP_FUNCTION(hello) { char *arg = "Hello my first extention!"; int len; char *strg; len = spprintf(&strg, 0, "%s\n", arg); RETURN_STRINGL(strg, len, 0); }保存后退出
四.编译代码
1.编译成so文件
cd /php-src/ext/rube whereis phpize看是否存在phpize
若是存在运行phpize,不然用
sudo apt-get install php5-dev 进行安装后运行 phpize
而后
./configure --with-php-config=你的php-config位置若是找不到php-config的位置
whereis php-config
./configure --with-php-config=你的php-config位置接着
make在编译过程当中若是你的代码出现错误,会报错。
make这步中若是提示
Build complete
说明编译成功.而后
make install
Installing shared extensions: /usr/lib/php5/20090626+lfs/
ls /usr/lib/php5/20090626+lfs/ #查看是否在此文件夹下
extension=/usr/lib/php5/20090626+lfs/rube.so #个人扩展在/usr/lib/php5/20090626+lfs/rube.so 你能够相应修改
vim test.php <?php echo hello();结果为 Hello my first extention