不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2016-01-09 11:18:47  修改时间:2024-05-12 11:22:53  分类:C/C++/Rust  编辑

Rango会讲解在Linux下从零开始写一个PHP扩展,并编译安装到PHP里,一直到执行扩展中的函数。包含的内容有:

  • 为什么要开发PHP扩展
  • ext_skel工具的使用
  • 修改config.m4
  • phpize的使用
  • 必备工具,autoconf / gcc / make / php5 / php5-dev
  • ZendAPI的使用简介,会介绍几个重要的API

摘自:http://wiki.swoole.com/wiki/page/238.html

 


学习笔记:

1、安装 Eclipse IDE 插件

(1) 去官网下载 Eclipse 3.7

http://www.eclipse.org/downloads/packages/release/indigo/sr2

(2) 安装 IDE插件

Help -> Install New Software

Work width -> 选择 “Indigo - http://download.eclipse.org/releases/indigo

Programming Languages ->

选择“C/C++ Development Tools
选择“PHP Development Tools(PDT) SDK Feature

(3) 下载PHP源码

http://php.net/releases/

2、安装开发工具(3个)

sudo apt-get install gcc make autoconf
  • gcc:C语言的编译器
  • make:C/C++工程自动构建工具
  • autoconf:检测一些系统环境,以及一些编译参数的设置

3、开发扩展

(1)  重要工具  ./ext/ext_skel

为我们自动创建一个PHP扩展工程的工具/脚本,执行它一下,会打印一些帮助信息

$ ./ext_skel  
./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 swoole if the module compiled

(2) 利用 ext_skel 创建工程

$ ./ext_skel --extname=swoole
Creating directory swoole
Creating basic files: config.m4 config.w32 .svnignore swoole.c php_swoole.h CREDITS EXPERIMENTAL swooles/001.phpt swoole.php [done].

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

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

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

(3) 进入到 扩展 swoole 目录中

config.m4:autoconf 工具的配置文件
php_swoole.h:头文件
swoole.c:C源文件

开发扩展的第一步,就是修改 config.m4,把我们的扩展启用,dnl 是 autoconf 的 注释符,把

dnl PHP_ARG_WITH(swoole, for swoole support,
dnl Make sure that the comment is aligned:
dnl [  --with-swoole             Include swoole support])

改为

PHP_ARG_WITH(swoole, for swoole support,
[  --with-swoole             Include swoole support])

表示 启用了这个扩展

(4) 在 扩展 swoole 目录下,执行命令 

$ /opt/software/php/bin/phpize 
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

PHP根据刚才修改的 config.m4 文件,生成一个 configure 脚本,执行成功后,会看到这个目录就多了一些文件。

我们最主要看的是 configure 这个文件,它是一个Shell脚本,这个脚本帮我们检测一些头文件,一些环境特性方面的东西。

(5) 执行 configure 文件

sudo ./configure  --with-php-config=/opt/software/php/bin/php-config

执行成功后,在扩展目录下会产生一个 Makefile 文件,它是 make 命令的配置文件,

(6) make 根据 Makefile 文件,对 C 源文件进行编译。

$ sudo make

会在扩展的 modules 目录下,生成 swoole.so 文件

(7) 安装扩展

将刚才编译好的扩展(swoole.so)安装到PHP的扩增目录

$ sudo make install
Installing shared extensions:     /opt/software/php/lib/php/extensions/no-debug-non-zts-20100525/

(8) 启用扩展

通过如下命令,找到 php.ini 文件位置,

$ /opt/software/php/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /opt/software/php/etc
Loaded Configuration File => /opt/software/php/etc/php.ini

编译 php.ini,添加

extension=swoole.so

(9) 查看扩展是否被启用

$ /opt/software/php/bin/php -m
[PHP Modules]
bcmath
bz2
...
sysvshm
swoole
tokenizer
...
zip
zlib

[Zend Modules]
Xdebug

(10) 自己重新写一个函数 swoole_hello

php_swoole.h  文件中,增加

PHP_FUNCTION(swoole_hello);

swoole.c 文件中,增加

PHP_FUNCTION(swoole_hello)
{
    long a;
    long b;
    char *c;
    int c_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &a, &b, &c, &c_len) == FAILURE) {
        return;
    }

    char *str;
    int len = spprintf(&str, 0, "%s: %d\n", c, a * b);

    RETURN_STRINGL(str, len, 0);
}

再重新编译、安装  sudo make && make install

(11) 检测 自定义函数 swoole_hello 是否存在

$ php --rf 'swoole_hello' 
Exception: Function swoole_hello() does not exist

提示 函数不存在,说明该函数还没有注册到 Zend引擎中

(12) 注册 swoole_hello函数到 Zend引擎中

在 swoole.c 文件中找到如下代码块:

/* {{{ swoole_functions[]
 *
 * Every user visible function must have an entry in swoole_functions[].
 */
const zend_function_entry swoole_functions[] = {
    PHP_FE(confirm_swoole_compiled,    NULL)        /* For swooleing, remove later. */
    PHP_FE_END    /* Must be the last line in swoole_functions[] */
};

增加 swoole_hello 函数的注册

const zend_function_entry swoole_functions[] = {
    PHP_FE(swoole_hello,    NULL)
    PHP_FE(confirm_swoole_compiled,    NULL)        /* For swooleing, remove later. */
    PHP_FE_END    /* Must be the last line in swoole_functions[] */
};

(13) 重新 编译、安装

$ sudo make && make install

(14) 重新 检测函数是否存在

$ php --rf 'swoole_hello'
Function [ <internal:swoole> function swoole_hello ] {
}

OK,函数注册Zend成功!

(15) 测试 php 函数

<?php

echo swoole_hello(2, 3, "data");

输出:

data: 6