设为首页 加入收藏

TOP

PHP扩展-扩展的生成和编译
2017-10-10 11:55:18 】 浏览:8847
Tags:PHP 扩展 生成 编译

首先说明一下,PHP扩展有两种编译方式:
方式一:在编译PHP时直接将扩展编译进去
方式二:扩展被编译成.so文件,在php.ini里配置加载路径;

以下开始说明创建PHP扩展并编译的步骤:
下载PHP源码,并解压,在源码的根目录下开始操作,
1. 使用ext_skel生成扩展框架,如下:

? php-5.6.24 cd ~/Downloads/tmp/php-5.6.24
? php-5.6.24 cd ext
? ext ./ext_skel --extname=myfirstext

 

ext_skel在执行后,会提示开发者后续的操作步骤,这个操作步骤是扩展的两种编译方式里的方式一的步骤, 如下:

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

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

2. 修改文件ext/myfirstext/config.m4
重点看line10-18的代码,用于设置./configure时启用此扩展的命令选项,将其中line16和line18的dnl删掉,把dnl理解为注释符。

14 dnl Otherwise use enable:
15
16 dnl PHP_ARG_ENABLE(myfirstext, whether to enable myfirstext support,
17 dnl Make sure that the comment is aligned:
18 dnl [ --enable-myfirstext Enable myfirstext support])
19
20 if test "$PHP_MYFIRSTEXT" != "no"; then
21 dnl Write more examples of tests here...

以上两步骤是公共的,以下将分别介绍编译PHP扩展的两种方式,
方式一:编译PHP时直接将扩展编译进去
3. 在源码根目录下执行./buildconf,如下
4. 在源码根目录下执行./configure –enable-myfirstext
为了减少编译时间,可以在configure阶段指明不编译某些模块,比如:

./configure --without-iconv --enable-debug --enable-myfirstext --disable-cgi --enable-cli --without-pear --disable-xml --without-mysql

5. 在源码根目录下执行make
注意编译成功后,别执行make install了,因为至此,扩展myfirstext已经编译成功,并且已经生成了相应的php二进制文件了,它在./sapi/cli/php

方式二:扩展被编译成.so文件,在php.ini里配置加载路径
3. 在扩展目录ext/myfirstext/下执行phpize命令
4. 在扩展目录ext/myfirstext/下执行./configure –enable-myfirstext命令
5. 在扩展目录ext/myfirstext/下执行make
执行make后会在ext/myfirstext/modules下生成对应的.so文件,在php.ini中配置好加载此文件即可。

校验扩展是否加载成功
执行./sapi/cli/php -f ext/myfirstext/myfirstext.php
或者通过php -m列出所有扩展,查看是否有myfirstext, 执行命令:./sapi/cli/php -m | grep myfirstext
通过以上校验,说明扩展编译成功了。但是到目前为止,还没有编辑过c相关的代码,一切都是ext_skel默认生成的,查看这个扩展myfirstext包含哪些函数呢?如下:

? php-5.6.24 ./sapi/cli/php -r 'print_r(get_extension_funcs("myfirstext"));'

OK, 目前为止熟悉了PHP扩展框架的生成,配置,和编译。接下来就要往扩展myfirstext里添加一个自己的函数。

 

qrcode_for_gh_61c6224cfae9_258

 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHP扩展-生命周期和内存管理 下一篇数据类型和进制转换

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目