php調用c介面無錯版介紹_PHP教程

來源:互聯網
上載者:User
1.首先是要安裝好PHP

2.進入PHP的下載解壓目錄下的ext目錄
#cd /root/php-5.3.6/ext
#./ext_skel --extname=hmc

說明:
./ext_skel --extname=module_name
module_name是你自己可以選擇的擴充模組的名字,例如我選擇的hmc。執行工具後會自動在ext目錄下建立你選擇的module_name名字的目錄,裡面已經產生了相關的代碼,這些代碼中只需要調整config.m4檔案中的三行注釋就可以正常的編譯帶這個自訂擴充模組的php了。

3.修改config.m4
可以將產生的module目錄複寫到任意希望放置的地方
進入hmc目錄
vi config.m4
使用文字編輯器開啟config.m4檔案
根據你自己的選擇將
dnl PHP_ARG_WITH(hmc, for hmc support,
dnl Make sure that the comment is aligned:
dnl [ --with-hmc Include hmc support])
修改成
PHP_ARG_WITH(hmc for hmc support,
Make sure that the comment is aligned:
[ --with-hmc Include hmc support])
或者將
dnl PHP_ARG_ENABLE(hmc, whether to enable hmc support,
dnl Make sure that the comment is aligned:
dnl [ --enable-hmc Enable hmc support])
修改成
PHP_ARG_ENABLE(hmc, whether to enable hmc support,
Make sure that the comment is aligned:
[ --enable-hmc Enable hmc support])

一般我會選擇後者,然後儲存退出。

4.修改.c和.h檔案代碼
Vi hmc.c
將檔案其中的下列代碼進行修改
/* Every user visible function must have an entry in hmc_functions[].
*/
const zend_function_entry hmc_functions[] = {
PHP_FE(say_hello, NULL) /* 添加著一行代碼 */
PHP_FE(confirm_hmc_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in hmc_functions[] */
};

在檔案的最後添加下列代碼
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}
儲存檔案退出

vi php_hmc.h
在檔案中PHP_FUNCTION(confirm_hmc_compiled);一行前面添加下面的代碼
PHP_FUNCTION(say_hello);
儲存檔案退出

5.編譯安裝
#phpize
#./configure --enable-hmc
#make
#make install
應該在php的modules目錄下有一個hmc.so檔案,不同的機器位置會不一樣



6.修改PHP配置
編輯php.ini,把擴充加入進去:
在[PHP]模組下增加:
extension = hmc.so
重啟Httpd
#service httpd restart

7.php調用
建立一個c.php檔案在對應的www目錄下
say_hello();
?>
這時瀏覽器應該輸出:
hello world

8.調試
可以在命令列下輸入
#php –q c.php
需要進入當前的www目錄下

查看是否安裝好模組可以用phpinfo()



也可以適用php -m命令
先用
#which php
找到php所在目錄,不同機器目錄不一樣
#/usr/bin/php -m


9.帶參數的函數
Vi hmc.c
修改最後的say_hello函數內容如下:
PHP_FUNCTION(say_hello)
{
zval **yourname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));
}

修改c.php為
say_hello(“清清月兒");
?>

http://www.bkjia.com/PHPjc/740212.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/740212.htmlTechArticle1.首先是要安裝好PHP 2.進入PHP的下載解壓目錄下的ext目錄 #cd /root/php-5.3.6/ext #./ext_skel --extname=hmc 說明: ./ext_skel --extname=module_name module_name是...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.