linux下 C語言對 php 擴充

來源:互聯網
上載者:User

一,搭建php環境
下載php 5.2.6 源碼 並解壓
編譯安裝,搭建php環境

二,建立擴充項目

進入源碼目錄
cd php5.2.6/ext/
./ext_skel --extname=my_ext
建立名字為my_ext的項目,最終會產生my_ext.so

三,更改配置和程式
$ vi ext/my_ext/config.m4

根據你自己的選擇將

dnl PHP_ARG_WITH(my_ext, for my_ext support,
dnl Make sure that the comment is aligned:

dnl [ --with-my_ext Include my_ext support])
修改成

PHP_ARG_WITH(my_ext, for my_ext support,
Make sure that the comment is aligned:

[ --with-my_ext Include my_ext support])
或者將

dnl PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,
dnl Make sure that the comment is aligned:

dnl [ --enable-my_ext Enable my_ext support])
修改成

PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,

Make sure that the comment is aligned:
[ --enable-my_ext Enable my_ext support])

$ vi ext/my_ext/php_my_ext.h


PHP_FUNCTION(confirm_my_ext_compiled); /* For testing, remove later. */
更改為
PHP_FUNCTION(say_hello);

$ vi ext/my_ext/my_ext.c


zend_function_entry php5cpp_functions[] = {
PHP_FE(confirm_my_ext_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
};
更改為
zend_function_entry php5cpp_functions[] = {
PHP_FE(say_hello, NULL)
{NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
};

在最後添加:
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}

四,編譯
$ cd my_ext
$ /usr/local/php/bin/phpize
ps: 如果出現:Cannot find autoconf.……的錯誤資訊,則需要安裝 autoconf (安裝過程略)
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make

這時會編譯出 my_ext/modules/my_ext.so

五,配置php.ini
將my_ext.so放入/usr/local/php/ext/目錄

$ vi php.ini

修改添加如下:
extension_dir = '/usr/local/php/ext/'
extension=my_ext.so

六,測試
$ vi test.php
<?php
say_hello();
?>

$ /usr/local/php/bin/php test.php
hello world.

則大功告成

ps:如有問題請留言,大家共同探討

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.