调试目标:执行php程序(php脚本里使用扩展里的函数等),c代码里面开始唤起单步调试,步骤简洁php
c开发工具:可以识别各类文件格式(m4,makefile),能处理好include的引用git
现代IDE提供的功能,搜索,跳转,终端,git等github
gdb调试比较繁琐json
尝试过clion, eclipse, atom, vscode, qtcreator
ubuntu
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
cd ext ./ext_skel --extname=sum cd sum
修改如下文件
config.m4api
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.cbash
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.phpeclipse
<?php $a = sum([1,2,3,4,10]); var_dump($a);
php test.php函数
会输出 int(20)工具
菜单栏:文件->新建文件或项目->导入现有项目
项目名称:php-src, 位置为php源码编译目录
默认
默认
构建配置:
补充:
清除步骤:
解决扩展引用头文件问题,添加"."
运行配置:
f5调试走起:
学习他人扩展约定:
把别人的扩展放到ext目录下,按图片修改下内容,按以上步骤调试
PHP Extension Development for Beginners with Joe Watkins
(https://www.youtube.com/watch...)