自己写个php的C扩展插件

php安装包下面的/ext/

新建预设文件
新建一个预定义文件:vim kq_sum_module.def

int kq_sum_module(int x, int y)

当然也可以不设置,后面自己加进去也可以,设置了,只是自动完成而已
将会在kq_sum_module.c生成

PHP_FUNCTION(kq_sum_module)
{
    int argc = ZEND_NUM_ARGS();
    zend_long x;
    zend_long y;

    if (zend_parse_parameters(argc, "ll", &x, &y) == FAILURE) 
        return;
    php_error(E_WARNING, "kq_sum_module: not yet implemented");
}
./ext_skel --extname=kq_sum_module --proto=kq_sum_module.def

我创建了一个kq_sum模块
输出》

[root@VM_0_7_centos ext]# ./ext_skel --extname=kq_sum_module --proto=kq_sum_module.def
Creating directory kq_sum_module
awk: /www/server/php/72/src/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .gitignore kq_sum_module.c php_kq_sum_module.h CREDITS EXPERIMENTAL tests/001.phpt kq_sum_module.php [done].

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

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

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

修改config.m4文件

将下面的 dnl 去掉

PHP_ARG_ENABLE(kq_sum_module, whether to enable kq_sum_module support,
dnl Make sure that the comment is aligned:
[  --enable-kq_sum_module           Enable kq_sum_module support])

得到

PHP_ARG_ENABLE(kq_sum_module, whether to enable kq_sum_module support,
Make sure that the comment is aligned:
[  --enable-kq_sum_module           Enable kq_sum_module support])

修改扩展原始文件kq_sum_module.c刚才预设的文件

PHP_FUNCTION(kq_sum_module)
{
    int argc = ZEND_NUM_ARGS();
    zend_long x;
    zend_long y;

    if (zend_parse_parameters(argc, "ll", &x, &y) == FAILURE) 
        return;
    long z;
    z = x + y;
    RETURN_LONG(z);
    php_error(E_WARNING, "kq_sum_module: not yet implemented");
}

安装扩展3部曲

  • phpize
  • ./configure --with-php-config=/www/server/php/72/bin/php-config
  • make
  • make install
  • 修改php.ini,引入kq_sum_module.so
extension = kq_sum_module.so

重启动php

打印phpinof

kq_sum_module

| kq_sum_module support | enabled |

搞定

使用

echo kq_sum_module(10,2);

return 12

猜你喜欢

转载自blog.csdn.net/weixin_33691700/article/details/87277820