php擴充,一個helloworld的實現
php -v
PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:47:39)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
配置參考文章:http://blog.csdn.net/niujiaming0819/article/details/8543028
本文也採用參考文章中的步驟過程為本機實現過程
step 1. 下載php源碼包,php官網下載即可,本次使用的是php5.6.8版本
step 2. 解壓壓縮包,進入解壓後的包(mysoft$ cd php-5.6.8/) ,同過 ls命令查看源碼包中的檔案目錄,進入ext檔案夾(cd ext)
step 3.建立擴充開發架構
./ext_skel --extname=helloworld
執行上面的命令建立擴張開發的架構,這是你會發現ext檔案夾下面多了一個叫helloworld的檔案夾,同時命令列也輸出了一些文本,
會提示你產生擴充的大致步驟。
step 4. 進入php源碼的根目錄, 編輯檔案 vim ext/helloworld/config.m4
去掉這個檔案中的幾行注釋大致會在第16-19行之間,具體版本也許不一樣之後儲存檔案(:wq)
step 5. 在php源碼根目錄執行命令 ./buildconf --force
step 6. 在php源碼的根目錄編譯php程式,注意命令為 ./configure --with-helloworld
最後出現的error,沒有處理
step 7. 進入我們的擴充目錄helloworld,執行命令 phpize(通過 sudo apt-get install php5-dev 安裝 phpize),此時你的擴充目錄會產生很多檔案,可以用於後期編譯。
step 8.在helloworld目錄編譯我們的擴充 ./configure --with-php-config=/usr/bin/php-config(使用你自己環境的php-config) --enable-helloworld
你可以通過一條命令尋找你的php-config檔案的位置(find / -name php-config)我的地址為/usr/bin/php-config
step 9 .進入擴充helloworld目錄,編輯檔案php_helloworld.h,在最後一行添加函數 PHP_FUNCTION(helloworldTest);
helloworldTest 可以改成你喜歡的名字,之後儲存退出
step 10. 用vim 開啟helloword.c,在helloworld.c中實現我們的函數,之後 將helloworldTest函數加入到helloworld_functions[]中,儲存退出
step 11.執行make命令 make 編譯擴充,我在啟動並執行過程中還是比較順利的。如果出現錯誤,請認真看前面步驟是否有錯,我在第一遍做的時候也有錯一般都是前面步驟有問題。
step12.將編譯產生的helloworld.so 檔案複製到你原生php擴充目錄
擴充目錄可以通過 php -r phpinfo(); | grep extension_dir 查看本機php的擴充路徑
step 13.配置php.ini 開啟 helloworld.so 擴充
查看php.ini 的位置(php -r phpinfo(); | grep php.ini )
vim 開啟 php.ini 檔案 sudo vim /etc/php5/cli/php.ini
在檔案最後添加擴充 (extension=helloworld.so)
通過 php -r phpinfo(); | grep helloworld 測試
step 14 .測試擴充(php -r echo helloworldTest();)
在這個過程中我曾按照原作者的步驟去嘗試,結果會出現一個錯誤
Segmentation fault” (core dumped)
經過調試,時原作者在helloworld.c 檔案中添加到helloworld_functions[] 時將 PHP_FE_END 換成了 {null,null,null} 而自己再編譯的時候並沒有出錯卻也給PHP_FE_END注釋 了,修改之後,重新編譯,複製就可以了。
http://www.bkjia.com/PHPjc/989127.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/989127.htmlTechArticlephp擴充,一個helloworld的實現 php -v PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:47:39) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2...