Heim  >  Artikel  >  Backend-Entwicklung  >  PHP扩展的生成和编译

PHP扩展的生成和编译

高洛峰
高洛峰Original
2016-11-17 14:20:111431Durchsuche

首先说明一下,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:

 $ cd ..
 $ vi ext/plogger/config.m4
 $ ./buildconf
 $ ./configure --[with|enable]-plogger
 $ make
 $ ./sapi/cli/php -f ext/plogger/plogger.php
 $ vi ext/plogger/plogger.c
 $ make

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

dnl Otherwise use enable:
16 dnl PHP_ARG_ENABLE(myfirstext, whether to enable myfirstext support,
dnl Make sure that the comment is aligned:
dnl [ --enable-myfirstext Enable myfirstext support])
20 if test "$PHP_MYFIRSTEXT" != "no"; then
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里添加一个自己的函数。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn