用qt creator搭建開發、調試php擴展的環境

操做系統:deepin(debain),ubuntu(請本身嘗試)

目標:使用qtcreator,搭建php擴展開發、調試環境

  • 調試目標:執行php程序(php腳本里使用擴展裏的函數等),c代碼裏面開始喚起單步調試,步驟簡潔php

  • c開發工具:可以識別各類文件格式(m4,makefile),能處理好include的引用git

  • 現代IDE提供的功能,搜索,跳轉,終端,git等github

背景: 

  • gdb調試比較繁瑣json

  • 嘗試過clion, eclipse, atom, vscode, qtcreator
     ubuntu

從源碼編譯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.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)工具

配置qt creator開發環境

菜單欄:文件->新建文件或項目->導入現有項目

導入一  

項目名稱:php-src, 位置爲php源碼編譯目錄
導入二

默認
導入三

默認
導入四

構建配置:

圖片描述  

補充:  

圖片描述  

清除步驟:  

圖片描述  

解決擴展引用頭文件問題,添加"."  

圖片描述  

運行配置:  

圖片描述

f5調試走起:  

圖片描述

約定

學習他人擴展約定:
把別人的擴展放到ext目錄下,按圖片修改下內容,按以上步驟調試

參考

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

相關文章
相關標籤/搜索