编写一个简单的PHP扩展

转自个人博客:

https://www.tanchengjin.com/article/109

 

测试环境ubuntu:14.04

 

首先在php官网下载php包

 

wget 
http://cn2.php.net/get/php-7.1.27.tar.gz/from/this/mirror

 

 

下载之后的包名位mirror 直接使用tar解压即可

tar -zxvf mirror

 

解压后进入ext文件夹

cd php-7.1.27/ext/

 

利用.ext_skel生成项目骨架,并在当前目录中生成helloWorld文件夹

./ext_skel --extname=helloWorld

 

提示生成扩展的步骤:

=======================================================

Creating directory helloWorld

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

 

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

 

1.  $ cd ..

2.  $ vi ext/helloWorld/config.m4

3.  $ ./buildconf

4.  $ ./configure --[with|enable]-helloWorld

5.  $ make

6.  $ ./sapi/cli/php -f ext/helloWorld/helloWorld.php

7.  $ vi ext/helloWorld/helloWorld.c

8.  $ make

 

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

编辑config.m4文件

vim config.m4

去掉这两行的dnl(dnl为注释符,这段话是说如果此扩展依赖其他扩展,去掉PHP_ARG_WITH段的注释符;否则去掉PHP_ARG_ENABLE段的注释符。显然我们不依赖其他扩展或lib库,所以去掉PHP_ARG_ENABLE段的注释符:)

image.png

 

编辑helloWorld.c文件

vim helloWorld.c

 

注释掉自动生成的方法

image.png

 

重写PHP_FUNCTION方法

image.png

 

添加到编译列表里,同时注释掉confirm_helloWorld_compiled

image.png

 

让zend引擎知道此模块有什么函数

保存退出

 

执行phpize

phpize

image.png

 

生成.so文件

./configure --with-php-config=/usr/bin/php-config

image.png

 

编译

make && make install

image.png

编译成功后会在项目文件夹/ext/helloWorld/modules下生成helloWorld.so文件

 

编写helloWorld.php文件,写入如下代码

<?php
 echo helloWorld();

执行 

php helloWorld.php

就可以看到执行结果

 

 

 

转自个人博客:

https://www.tanchengjin.com/article/109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值