开发环境:mac OS X EI Captionphp
IDE:Ecplise c/c++插件c++
php版本:5.5.36(当前的php版本>=开发版本)api
进入到ext扩展
目录下服务器
执行ext_skel
脚本,能够看到提示信息函数
--extname=module module is the name of your extension
插件
在次执行./ext_skel --extname=test
指针
能够看到目录下新增了一个test
文件夹code
进入test文件夹下,修改config.m4
配置文件开发
dnl PHP_ARG_WITH(test, for test support, dnl Make sure that the comment is aligned: dnl [ --with-test Include test support]) //dnl表示注释须要取消掉 PHP_ARG_WITH(test, for test support, [ --with-test Include test support])
执行phpize
,能够看到多出了configure
文件,能够帮助咱们检测头文件,环境字符串
执行configure
文件,此处应该指向你的php-config
目录
./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config
执行sudo make install
安装扩展
执行php -i | grep php.ini
找到php.ini文件
在php.ini
文件上加上test.so
重启服务器
执行php -m
看到test
扩展就成功了
在php_test.h
头中声明一个函数PHP_FUNCTION(test);
在test.c
中实现这个函数
PHP_FUNCTION(test){ /*定义一个int 型变量*/ long a; long b; char *c; /* 字符串在c中使用指针,而且须要指定长度*/ int c_len; /* 此处字符串一个s 对应两个参数*/ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &a, &b,&c,&c_len) == FAILURE) { return; } char * str; int len = spprintf(&str, 0, "%s:%d\n",c,a*b); /*能够在zend_api下查看*/ RETURN_STRINGL(str, len, 0); }
注意,须要将test函数注册到zend_api中,不然会报not found
const zend_function_entry test_functions[] = { /*扩展定义的函数*/ PHP_FE(test,NULL) PHP_FE_END /* Must be the last line in test_functions[] */ };
从新
sudo make && make install
在php 文件中写下echo test(100,200,"data");
得出 data=20000既成功