PHP头条
热点:

VC建立php扩张


VC 建立php扩展

软件 php的源文件和安装包要一致

php5.3.8(VC9 x86 Thread Safe)
php5.3.8源文件(tar.bz2)
VC

bison.exe

MSYS(MSYS类似于Cygwin,但是由于工作原理的不同,速度更快、体积更小、功能强大、便于携带http://code.google.com/p/msys-cn/)

因为我的开发只是一个很简单的demo,没有使用第三方了类库。如果是把linux下拿过来的扩展项目,可能用到一些库。可能用cygwin会比较好。但是没有cygwin完全可以在window下开发。

1。解压下载到的源文件包tar.bz2包到C盘c:/phpsrc,并且解压php安装包(VC9 x86Thread Safe,也就是能够正常使用的php压缩包文件)到C:/php,我们只需要里面的一个文件C:/php/dev/ php5.lib,复制php5.lib到c:/phpsrc。

2。复制bison.exe到Microsoft Visual Studio\Common\MSDev98\Bin把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量

3 .在这里我们开始生成生成config.w32.h。CMD在里面操作

进入:c:/phpsrc执行

C:\phpsrc>buildconf
Rebuilding configure.js
Now run 'configure --help'

?建立一个临时环境变量

C:\phpsrc>set path=%path%;C:/phpsrc/bin

C:\phpsrc>cscript /nologo configure.js --with-php-build="../phpsrc" --without-libxml --disable-odbc

如果想要Non Thread Safe 模式就去掉上面的命令最后的参数--disable-zts

Saving configure options to config.nice.bat
Checking for cl.exe ...  
  Detected compiler MSVC9 (Visual C++ 2008)
  Detected 32-bit compiler
Checking for link.exe ...  C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN
Checking for nmake.exe ...  
Checking for lib.exe ...  
...........中间省略.............
-------------------------------------------
|               |                         |
-------------------------------------------
| Build type    | Release                 |
| Thread Safety | Yes                     |
| Compiler      | MSVC6 (Visual C++ 6.0)  |
| Architecture  | x86                     |
-------------------------------------------

Type 'nmake' to build PHP

如果出现上类似的提示, 说明你的PHP开发环境已经搭建成功,同时在main下面多了一个 config.w32.h。即PHP开发环境的配置文件。如果没有这个脚本,windows下开发php,简直太悲惨了。

?

4.开发PHP扩展的方法

到这里我们就可以建立开发PHP扩展了,但是无从下手,我们该怎么办,其实PHP也给我们提供了很好用的工具来建立一个PHP扩展的骨架。就是用C:\phpsrc\ext下的ext_skel_win32.php如何运行,以php结尾,说明依赖php。下面看具体方法,我建立一个名为’test‘的扩展。这里要确保你的php.exe可以直接在cmd下使用,具体还是加入Path路径 。虽然有ext_skel_win32.php,用php执行,但是我们不知道具体的内容。 其实执行如下命令:

C:\phpsrc\ext>php ext_skel_win32.php
'sh' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

ext_skel_win32.php源码中说要使用cygwin,但我机器上没有装cygwin,另外发现其中实际上只使用到了sh,而我机器上装的MSYS里也有sh,应该可以用的吧,于是就将ext_skel_win32.php中的$cygwin_path变量设置成了MSYS的BIN目录

$cygwin_path = 'c:\msys\1.0\bin';
?
C:\phpsrc\ext>php ext_skel_win32.php
ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

PHP Warning:  fopen(/.php): failed to open stream: No such file or directory in
C:\phpsrc\ext\ext_skel_win32.php on line 52

C:\phpsrc\ext>

?可以看到如何使用这php脚本。大体命令如下

c:\phpsrc\ext>php ext_skel_win32.php  --extname=test
Creating directory test
Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [do
ne].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/test/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-test
5.  $ make
6.  $ ./php -f ext/test/test.php
7.  $ vi ext/test/test.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/test/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

c:\phpsrc\ext>

这样就成功创建了一个php扩展骨架 ,你可以在里面修改。具体的还要好好研究PHP API。具体扩展的位置就在ext目录下,打开可以看到test文件夹,这就是刚才命令创建的。

C:\phpsrc\ext\test 的目录

2011-11-29  16:57              .
2011-11-29  16:57              ..
2011-11-29  16:57                16 .svnignore
2011-11-29  16:57             1,970 config.m4
2011-11-29  16:57               282 config.w32
2011-11-29  16:57                 4 CREDITS
2011-11-29  16:57                 0 EXPERIMENTAL
2011-11-29  16:57             2,768 php_test.h
2011-11-29  16:57             5,128 test.c
2011-11-29  16:57             4,954 test.dsp
2011-11-29  16:57               500 test.php
2011-11-29  16:57              tests
               9 个文件         15,622 字节
               3 个目录  7,441,883,136 可用字节

C:\phpsrc\ext\test>

这样一个php扩展的框架已经创建完成了。

?

下面就是配置使用vc++6开发这个扩展

III. 添加依赖的php5ts.lib
? 在php的二进制包中的 dev目录下将 php5ts.lib 拷到我们的test目录中, 否则编译将通不过。
IV. 添加test c代码
生成的test目录中有关键文件包括
? test.dsp,
? test.c,
? php_test.h,
其他文件暂时不必关心.

提示:切忌test目录不可以挪移出ext目录,否则会编译报缺少php.h.

1. 修改php_test.h
扩展的新函数: 在PHP_FUNCTION(confirm_test_compiled); 行后添加一行

PHP_FUNCTION(confirm_test_compiled);
PHP_FUNCTION(test);  // 新增的行

2. 修改test.c
在PHP_FUNCTION(confirm_test_compiled) 后添加我们的新函数

PHP_FUNCTION(test){
    php_printf("Hello C extension");
}

在数组zend_function_entry test_functions[]增加一行

const zend_function_entry test_functions[] = {
	PHP_FE(confirm_test_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(test, NULL) // 新增的行
	PHP_FE_END	/* Must be the last line in test_functions[] */
};

?V. 构建DLL文件

用vc6打开我们的工程,就是test.dsp
1. 修改编译方式为release: 选择Build->Set Active Configuration设置默认编译方式为Release, 否则会提示缺少php5ts_debug.lib ,其实就是php5ts.lib。
2. 按F5编译。会在ext上级的Release_TS目录下生成php_test.dll

提示:如果愿意使用命令行编译也是可以的,命令如下:

msdev test\test.dsp /MAKE "test - Win32 Release_TS" 

?VI. 集成dll到php中。

?

www.phpzy.comtrue/phprm/56014.htmlTechArticleVC建立php扩张 VC 建立php扩展 软件 php的源文件和安装包要一致 php5.3.8(VC9 x86 Thread Safe) php5.3.8源文件(tar.bz2) VC bison.exe MSYS(MSYS类似于Cygwin,但是由于工作原理的不同,速度更快、体积更小...

相关文章

PHP之友评论

今天推荐