調試目標:執行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...)