操作系统: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

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

var_dump($a);

php test.php

会输出 int(20)

配置qt creator开发环境

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

4fbf1a431b42650645838f26e7a282e8.png

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

33541df838af72c90fca3ce1c9c25f54.png

默认

3a516146fcee35fe65cfd8e388013aa7.png

默认

i6Jz6v.png

构建配置:

iiumAr.png

补充:

3ede14d40b33f2238c8969fce56d32f4.png

清除步骤:

fmQrey.png

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

6bd0d9694e261183c43cdc7ea3758f2e.png

运行配置:

a81e7196bcb6725d35b4f5bdb4304f3d.png

f5调试走起:

5eb9c5d8aa9bf1b312b00df0340ffc61.png

约定

学习他人扩展约定:

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

参考

PHP Extension Development for Beginners with Joe Watkins

(https://www.youtube.com/watch…)

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐