1、目的
为了在php中使用C语言的扩展,本文介绍在windows系统下,将C扩展打包成dll文件,提供给php调用的方法
Linux系统下的方法见:http://c.biancheng.net/cpp/html/1400.html
2、需要安装的软件
(1)wamp server:其中包含php,本文中php版本为5.5.12
安装路径如:C:\wamp\,其中C:\wamp\bin\php\php5.5.12为php所在路径,将其加入环境变量Path
(2)Visual Studio:本文中版本为VS2013
安装路径如:C:\Program Files (x86)\Microsoft Visual Studio 12.0\
将C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin; 和 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE; 加入环境变量Path
否则之后会报找不到C编译器的错误:MS C++ compiler is required
(3)Cygwin:本文中版本为2.738,为了在windows下模拟unix环境,下载及安装流程见:http://www.cr173.com/soft/60977.html
3、下载未编译的php源码包
官方下载地址:http://www.php.net/downloads.php,选择适当的版本和下载点下载,本文用的版本是5.5.26
下载后解压源码包,找到./ext目录,这个是负责开发扩展的目录,有两个主要用到的文件,ext_skel,ext_skel_win32.php,
ext_skel是创建扩展的shell,在windows上无法运行,所以要有Cygwin
下载后,将安装目录下的所有源码文件拷贝到C:\wamp\bin\php\php5.5.12\下
4、修改Cygwin路径
如果你的Cygwin没有安在c:\cygwin,进入./ext目录下,修改ext_skel_win32.php
将$cygwin_path = 'c:\cygwin\bin';
修改为你的cygwin安装目录
5、写C语言函数声明文件
在ext目录下新建函数声明文件,如:myfunc.def
文件内声明C语言扩展对php提供的函数原型,如:
int a(int x,int y)
string b(string x, string y)
写声明文件的目的是在下一步建立骨架目录时,目录内的.c文件内会生成相应函数的部分代码,方便后续编写。
这一步也可以省略,那么需要在第7步添加额外的代码
6、建立php扩展骨架目录
打开Cygwin,cd到ext目录c:/wamp/bin/php/php5.5.12/ext,根据myfunc.def建立骨架目录:
php ./ext_skel_win32.php --extname=myfunc --proto=myfunc.def
若没有生成def文件,不需要最后一个参数
Warning: fopen(myhello/myhello.dsp): failed to open stream: No such file or directory in D:\cygwin\php-5.2.6\ext\ext_skel_win32.php on line 45
Warning: fopen(myhello/myhello.php): failed to open stream: No such file or directory in D:\cygwin\php-5.2.6\ext\ext_skel_win32.php on line 52
说明Cygwin安装不完整,或者命令有错,或者是目录下已经有myfunc文件夹了。检查是否是上述错误后,重启cygwin再试试。
若执行报错php不是系统命令,将php所在路径C:\wamp\bin\php\php5.5.12加入环境变量Path并重启
执行成功显示以下信息:
$ php ./ext_skel_win32.php --extname=myfunc --proto=myfunc.def
Creating directory myfunc
awk: /cygdrive/c/wamp/bin/php/php5.5.12/ext/skeleton/create_stubs:56: w
scape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .svnignore myfunc.c php_myfu
ITS EXPERIMENTAL tests/001.phpt myfunc.php [done].
To use your new extension, you will have to execute the following steps
1. $ cd ..
2. $ vi ext/myfunc/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-myfunc
5. $ make
6. $ ./sapi/cli/php -f ext/myfunc/myfunc.php
7. $ vi ext/myfunc/myfunc.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/myfunc/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writ
code and repeat the last two steps as often as necessary.
7、添加C语言代码
修改ext/myfunc/myfunc.c文件,修改PHP_FUNCTION(a)和PHP_FUNCTION(b):
PHP_FUNCTION(a)
{
int argc = ZEND_NUM_ARGS();
long x;
long y;
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
return;
int z = x+y;
RETURN_LONG(z);
//php_error(E_WARNING, "a: not yet implemented");
}
/* }}} */
/* {
{
{ proto string b(string x, string y)
; */
PHP_FUNCTION(b)
{
char *x = NULL;
char *y = NULL;
int argc = ZEND_NUM_ARGS();
int x_len;
int y_len;
if (zend_parse_parameters(argc TSRMLS_CC, "ss", &x, &x_len, &y, &y_len) =