C php 扩展

环境及工具:

  • 操作系统:centos7,建议用root账户(个人习惯)
  • 安装eclipse c/c++环境:http://blog.csdn.net/phper_man/article/details/51711918
  • 安装一些编译依赖的工具:yum install gcc make autoconf
  • 安装LNMP环境,参照:http://lnmp.org/install.html;
  • 下载最新版PHP源文件,解压:http://php.net/ 【选择版本要和lnmp选择的版本相同】
    • 找到PHP7的中国镜像下载:http://php.net/get/php-7.0.7.tar.gz/from/a/mirror
    • 解压后先留后面用

查看PHP有关信息:

[root@localhost ~]# php -v
PHP 7.0.7 (cli) (built: Jun 19 2016 17:21:21) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
[root@localhost ~]# php --ini
Configuration File (php.ini) Path: /usr/local/php/etc
Loaded Configuration File:         /usr/local/php/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)
[root@localhost soft]# 

操作流程:

  • 在eclipse中创建一个新C工程php7
  • 将刚才下载的源文件复制到工程目录
  • 在eclipse里刷新即可看到全部源文件
  • 返回命令模式中,cd ext && ls 看到有个工具:ext_skel
  • ./ext_skel 可看到一些帮助信息
  • 创建新工程: ./ext_skel –extname=test【extname前是两个减号】
  • 查看新配置文件:cd test && ls
  • 返回eclipse,刷新ext目录,往下找到test目录

修改config.m4文件:

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

找到–with-test部分,去掉前面dnl,这里改为

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

在命令下操作编译:

[root@localhost test]# phpize
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
[root@localhost test]# ./configure --with-php-config=/usr/local/php/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
【这儿省略若干行信息】
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
[root@localhost test]# make
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'.
[root@localhost test]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
[root@localhost test]# 
[root@localhost test]# gedit /usr/local/php/etc/php.ini
【在php.ini中加入        extension=test.so   】
[root@localhost test]# php -m
【查看有没有加载test扩展】

测试是否成功,写一个PHP文件

-【为了操作方便,重开一个窗口,下面的操作是在不同目录窗口里操作的】:

[root@localhost ~]# cd /home/wwwroot/default/
[root@localhost default]# vi test_sum.php
<?php echo date("Y-m-d H:i:s")."\n"; echo confirm_test_compiled('haha');

运行PHP:

[root@localhost default]# php test_sum.php
2016-06-19 18:09:59
Congratulations! You have successfully modified ext/test/config.m4. Module haha is now compiled into PHP.

写c函数:

编辑test.c:
- 先删除原来默认的confirm_test_compiled函数;
- 在文件里加入下面函数:

PHP_FUNCTION(test_sum)
{
    char *arg = NULL;
    size_t arg_len, len;
    int a;
    int b;
    zend_string *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "lls",&a,&b, &arg, &arg_len) == FAILURE) {
        return;
    }

    strg = strpprintf(0, "Hello %.78s %d",arg,a+b);

    RETURN_STR(strg);
}
  • 往下面找到const zend_function_entry fazo_functions:注册函数
const zend_function_entry fazo_functions[] = {
    PHP_FE(test_sum,    NULL)       /* 把原来confirm_test_compiled改为test_sum */
    PHP_FE_END  /* Must be the last line in fazo_functions[] */
};

重新编译:

[root@localhost test]# make clean && make && make install
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:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/

再编辑刚才的PHP文件,改为:

[root@localhost default]# vi test_sum.php
<?php echo date("Y-m-d H:i:s")."\n test:\t"; echo test_sum(11,22,'11+22=');

运行PHP:

[root@localhost default]# php test_sum.php
2016-06-19 20:11:59
test:   Hello 11+22=33
[root@localhost default]# 

但是:以上PHP在cli下能打印出来,若用HTTP访问,函数返回部分是空的,还不知道是什么原因,有待继续找原因。

备注:

  • 本文核心部分参照Rango视频教程,并结合本人实践整理而成;
  • Rango在视频中的示例是基于php5,我是在PHP7下搞的,所以C函数有所不同,我也只是用PHP自动给的函数改了一下。
  • 视频原文:http://wiki.swoole.com/wiki/page/238.html

你可能感兴趣的:(eclipse,PHP,C语言,扩展)