php c扩展的方式,php中使用C语言写扩展的方法
php中使用C語言寫擴展的方法
發布時間:2020-08-20 15:49:21
來源:億速云
閱讀:102
作者:小新
小編給大家分享一下php中使用C語言寫擴展的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1、在php源碼路徑的ext文件夾下,新建一個extend_test.def文件,編輯文件內容為:string my_test_function(string str,int n)
2、在當前目錄執行./ext_skel --extname=extend_test --proto=extend_test.def
其中,extname是擴展的名,proto是剛創建的文件,也可以用全路徑
3、在當前目錄下會生成extend_test文件夾,編輯extend_test文件夾下的config.m4
去掉一下三行的dnl的注釋PHP_ARG_ENABLE(extend_test, whether to enable extend_test support,
Make sure that the comment is aligned:
[ --enable-extend_test Enable extend_test support])
4、編輯extend_test文件夾下的extend_test.c文件
找到以下方法并修改PHP_FUNCTION(my_test_function)
{
char *str = NULL;
int argc = ZEND_NUM_ARGS();
size_t str_len;
zend_long n;
char *result;
char *ptr;
zend_long result_length;
if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
return;
result_length = str_len * n;
result = (char *) emalloc(result_length + 1);
ptr = result;
while (n--) {
memcpy(ptr, str, str_len);
ptr += str_len;
}
*ptr = '\0';
RETURN_STRINGL(result, result_length);
}
5、生成擴展:
在extend_test文件夾下,運行/home/php/bin/phpize(實際為phpize所在路徑)
然后運行./configure --with-php-config=/home/php/bin/php-config(實際為php-config所在路徑)
6、編譯安裝make
make install
7、執行完后會顯示擴展安裝到了哪個路徑下
然后修改php.ini增加擴展信息extension=extend_test.so
8、測試擴展是否可用
新建test.php文件并將內容編輯為<?php
echo my_test_function('a',5);
保存后用php運行,顯示出aaaaa表示擴展成功安裝
以上是php中使用C語言寫擴展的方法的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
總結
以上是生活随笔為你收集整理的php c扩展的方式,php中使用C语言写扩展的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可视化文本操作
- 下一篇: 可视化GDI操作题目