用qt creator搭建开发、调试php扩展的环境

操作系统:deepin(debain),ubuntu(请自己尝试)

目标:使用qtcreator,搭建php扩展开发、调试环境

  • 调试目标:执行php程序(php脚本里使用扩展里的函数等),c代码里面开始唤起单步调试,步骤简洁

  • c开发工具:能够识别各种文件格式(m4,makefile),能处理好include的引用

  • 现代IDE提供的功能,搜索,跳转,终端,git等

背景: 

  • gdb调试比较繁琐

  • 尝试过clion, eclipse, atom, vscode, qtcreator
     

从源码编译php

apt-get install build-essential

#找个自己喜欢的目录(要有权限奥)

git clone https://github.com/php/php-src.git
cd php-src


./configure --help
./buildconf --force
./configure --disable-all --prefix=/usr --with-config-file-path=/etc/php.ini --with-config-file-scan-dir=/etc/php.d --enable-ctype --enable-json=shared
 make -j$(cat /proc/cpuinfo |grep "processor"|wc -l)
 ./sapi/cli/php -v
 make install
 php -v
 php -m
 php --ini
php -dextension=json.so -m
mkdir /etc/php.d
echo "extension=json.so" > /etc/php.d/json.ini
php -m

开发php扩展,请参考扩展开发文章 (sum)

cd ext
./ext_skel --extname=sum
cd sum

修改以下文件
config.m4

dnl $Id$
dnl config.m4 for extension sum

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:
PHP_ARG_WITH(sum, for sum support,
[  --with-sum             Include sum support])
if test "$PHP_SUM" != "no"; then
  PHP_NEW_EXTENSION(sum, sum.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi

sum.c

PHP_FUNCTION(sum)
{
    zval *input;
    zval *item;
    int32_t total=0;
    HashPosition position;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) {
        return;
    }

    for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &position);
      (item = zend_hash_get_current_data_ex(Z_ARRVAL_P(input), &position));
      zend_hash_move_forward_ex(Z_ARRVAL_P(input),&position)
     ){
        if(Z_TYPE_P(item) == IS_LONG){
            total += Z_LVAL_P(item);
        }else zend_throw_exception(NULL, "oh dear!", 0);
           
    }
    RETURN_LONG(total);
}

安装扩展

phpize
./configure
make
make install
echo "extension=sum.so" > /etc/php.d/sum.ini

查看扩展功能

test.php

<?php

$a = sum([1,2,3,4,10]);
var_dump($a);

php test.php

会输出 int(20)

配置qt creator开发环境

菜单栏:文件->新建文件或项目->导入现有项目

导入一  

项目名称:php-src, 位置为php源码编译目录
导入二

默认
导入三

默认
导入四

构建配置:

图片描述  

补充:  

图片描述  

清除步骤:  

图片描述  

解决扩展引用头文件问题,添加"."  

图片描述  

运行配置:  

图片描述

f5调试走起:  

图片描述

约定

学习他人扩展约定:
把别人的扩展放到ext目录下,按图片修改下内容,按以上步骤调试

参考

PHP Extension Development for Beginners with Joe Watkins
https://www.youtube.com/watch...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值