随笔日记

使用C编写一个自己的PHP扩展

2019-09-07 15:34:15

以最经典hello world!为例子,下面开始编写一个PHP扩展

1、下载php代码,并且解压,我这里的不是下面的这个PHP版本,我是基于宝塔的PHP7.2.1进行安装测试的

http://php.net/get/php-7.2.1.tar.bz2/from/a/mirror

具体PHP怎么安装,百度一下就知道了

新建一个PHP扩展,需要进入php中的ext目录,然后执行

./ext_skel --extname=iszmxw

由于我使用的是宝塔面板,安装的是PHP7.2.13,默认安装目录为“/www/server/php/72/src”,进入到相对应目录下

cd ext/
./ext_skel --extname=iszmxw

参考地址:https://www.php.net/manual/zh/internals2.buildsys.skeleton.php

执行完上面的命令后会生成一个iszmxw的扩展文件夹,同时又如下提示

Creating directory iszmxw
Creating basic files: config.m4 config.w32 .gitignore iszmxw.c php_iszmxw.h CREDITS EXPERIMENTAL tests/001.phpt iszmxw.php [done].

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

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

Repeat steps 3-6 until you are satisfied with ext/iszmxw/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.

然后cd iszmxw/ 进入扩展目录,ll -a,显示如下内容

[root@izwz92vqrxu3bz3edk0mytz iszmxw]# ll -a
total 44
drwxr-xr-x  3 root root 4096 Sep  7 15:02 .
drwxrwxr-x 77 root root 4096 Sep  7 15:02 ..
-rw-r--r--  1 root root 2824 Sep  7 15:02 config.m4
-rw-r--r--  1 root root  352 Sep  7 15:02 config.w32
-rw-r--r--  1 root root    7 Sep  7 15:02 CREDITS
-rw-r--r--  1 root root    0 Sep  7 15:02 EXPERIMENTAL
-rw-r--r--  1 root root  392 Sep  7 15:02 .gitignore
-rw-r--r--  1 root root 5152 Sep  7 15:02 iszmxw.c
-rw-r--r--  1 root root  502 Sep  7 15:02 iszmxw.php
-rw-r--r--  1 root root 2315 Sep  7 15:02 php_iszmxw.h
drwxr-xr-x  2 root root 4096 Sep  7 15:02 tests

此时证明扩展框架基本文件已经生成,下面进行修改配置文件


vim config.m4

# 将其中的下面的代码(删除dnl)

dnl PHP_ARG_ENABLE(iszmxw, whether to enable iszmxw support,

dnl Make sure that the comment is aligned:

dnl [  --enable-iszmxw           Enable iszmxw support])

# 修改成

PHP_ARG_ENABLE(iszmxw, whether to enable iszmxw support,

[  --enable-iszmxw           Enable iszmxw support])

增加函数申明

vim php_iszmxw.h

# 在其中增加 PHP_FUNCTION(iszmxw_helloworld);

vim iszmxw.c

# 将如下代码中的PHP_FE和PHP_FE_END中加入下面代码(这的代码是将函数指针注册到Zend引擎)

PHP_FE(iszmxw_helloworld,  NULL)

# 最后在iszmxw.c文件底部增加新的函数方法iszmxw_helloworld

PHP_FUNCTION(iszmxw_helloworld) {
php_printf("Hello World!\n"); } # 退出保存

到此为止,配置基本上已经修改完了,下面就是开始打包编译成.so扩展文件了

[root@izwz92vqrxu3bz3edk0mytz iszmxw]# phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@izwz92vqrxu3bz3edk0mytz iszmxw]# ./configure --with-php-config=/www/server/php/72/bin/php-config 
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /www/server/php/72
checking for PHP includes... -I/www/server/php/72/include/php -I/www/server/php/72/include/php/main -I/www/server/php/72/include/php/TSRM -I/www/server/php/72/include/php/Zend -I/www/server/php/72/include/php/ext -I/www/server/php/72/include/php/ext/date/lib
checking for PHP extension directory... /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718
checking for PHP installed headers prefix... /www/server/php/72/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking whether to enable iszmxw support... yes, shared
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

# 下面是安装执行生成so文件
[root@izwz92vqrxu3bz3edk0mytz iszmxw]# make && make install
/bin/sh /www/server/php/72/src/ext/iszmxw/libtool --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/www/server/php/72/src/ext/iszmxw -DPHP_ATOM_INC -I/www/server/php/72/src/ext/iszmxw/include -I/www/server/php/72/src/ext/iszmxw/main -I/www/server/php/72/src/ext/iszmxw -I/www/server/php/72/include/php -I/www/server/php/72/include/php/main -I/www/server/php/72/include/php/TSRM -I/www/server/php/72/include/php/Zend -I/www/server/php/72/include/php/ext -I/www/server/php/72/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /www/server/php/72/src/ext/iszmxw/iszmxw.c -o iszmxw.lo 
mkdir .libs
 cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/www/server/php/72/src/ext/iszmxw -DPHP_ATOM_INC -I/www/server/php/72/src/ext/iszmxw/include -I/www/server/php/72/src/ext/iszmxw/main -I/www/server/php/72/src/ext/iszmxw -I/www/server/php/72/include/php -I/www/server/php/72/include/php/main -I/www/server/php/72/include/php/TSRM -I/www/server/php/72/include/php/Zend -I/www/server/php/72/include/php/ext -I/www/server/php/72/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /www/server/php/72/src/ext/iszmxw/iszmxw.c  -fPIC -DPIC -o .libs/iszmxw.o
/bin/sh /www/server/php/72/src/ext/iszmxw/libtool --mode=link cc -DPHP_ATOM_INC -I/www/server/php/72/src/ext/iszmxw/include -I/www/server/php/72/src/ext/iszmxw/main -I/www/server/php/72/src/ext/iszmxw -I/www/server/php/72/include/php -I/www/server/php/72/include/php/main -I/www/server/php/72/include/php/TSRM -I/www/server/php/72/include/php/Zend -I/www/server/php/72/include/php/ext -I/www/server/php/72/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -o iszmxw.la -export-dynamic -avoid-version -prefer-pic -module -rpath /www/server/php/72/src/ext/iszmxw/modules  iszmxw.lo 
cc -shared  .libs/iszmxw.o   -Wl,-soname -Wl,iszmxw.so -o .libs/iszmxw.so
creating iszmxw.la
(cd .libs && rm -f iszmxw.la && ln -s ../iszmxw.la iszmxw.la)
/bin/sh /www/server/php/72/src/ext/iszmxw/libtool --mode=install cp ./iszmxw.la /www/server/php/72/src/ext/iszmxw/modules
cp ./.libs/iszmxw.so /www/server/php/72/src/ext/iszmxw/modules/iszmxw.so
cp ./.libs/iszmxw.lai /www/server/php/72/src/ext/iszmxw/modules/iszmxw.la
PATH="$PATH:/sbin" ldconfig -n /www/server/php/72/src/ext/iszmxw/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /www/server/php/72/src/ext/iszmxw/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/

# 上面是打包生成的so文件所在目录

通过上面就可以看到打包编译成功的so文件了,接下来就是修改php.ini文件配置,然后重启PHP使其生效

首先查看一下php.ini所在的位置

执行查看文件位置:php --ini

[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]# php --ini
Configuration File (php.ini) Path: /www/server/php/72/etc
Loaded Configuration File:         /www/server/php/72/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)
# 然后直接vim编辑文件
[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]# vim /www/server/php/72/etc/php.ini

# 找到extension的相关配置位置修改引入扩展文件目录,以及要引用的扩展文件名称

extension_dir = "/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/"

extension = iszmxw.so
# 退出保存,重启PHP,php -m查看扩展是否多了一个iszmxw的扩展

[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]# /etc/init.d/php-fpm-72 restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]# php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
filter
ftp
gd
gettext
hash
iconv
intl
iszmxw
json
libxml
mbstring
.......
.......
# 查看到了扩展模块后,检测自定义函数是否可用

[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]# php -r "iszmxw_helloworld();"
Hello World!
[root@izwz92vqrxu3bz3edk0mytz no-debug-non-zts-20170718]#

使用phpinfo查看扩展,检测到了安装的扩展



统计:
0 评论
0 查看


马上批阅

评论该篇文章

Laravel技术交流群
扫码关注体验小程序
小程序二维码 小程序码
站长最新说说动态

欢迎来到追梦小窝的博客,有什么值得收藏的就拿去用吧,不客气,大部分内容来自互联网,如有侵犯版权请您注明来信,我将会第一时间妥善处理


最新评论
共6条评论
image
2017-07-07 14:04:55 追梦小窝 评论了 今天画了一个小图标
来自:59.40.248.60
@皮皮豪:我QQ543619552
image
2017-07-07 14:04:27 追梦小窝 评论了 emlog缩略图不同实现方法:正文-附件-随机图片
来自:59.40.248.60
@皮皮豪:那里不会,教程说的很详细
image
2017-07-07 12:06:08 皮皮豪 评论了 emlog缩略图不同实现方法:正文-附件-随机图片
来自:120.82.74.53
看了还是不会啊
image
2017-07-07 18:23:25 皮皮豪 评论了 今天画了一个小图标
来自:120.82.74.36
怎么联系你啊
image
2017-07-07 12:07:49 追梦小窝 评论了 今天画了一个小图标
来自:113.91.34.248
@文森:别笑话我了
image
2017-04-04 22:29:04 文森 评论了 今天画了一个小图标
来自:223.74.150.13
这个图标看上去很nice
站点统计
  • 文章总数: 141篇
  • 微语总数: 6条
  • 评论总数: 6条
  • 运行天数: 1513天