php怎么运行c语言?php调用C代码的实现方法

四月 21, 2019 | views
Comments 0

在php程序中需要用到C代码,应该是下面两种情况:

1 已有C代码,在php程序中想直接用

2 由于php的性能问题,需要用C来实现部分功能

针对第一种情况,最合适的方法是用system调用,把现有C代码写成一个独立的程序。参数通过命令行或者标准输入传入,结果从标准输出读出。其次,稍麻烦一点的方法是C代码写成一个daemon,php程序用socket来和它进行通讯。

重点讲讲第二种情况,虽然沿用system调用的方法也可以,但是想想你的目的是优化性能,那么频繁的起这么多进程,当然会让性能下降。而写daemon的方法固然可行,可是繁琐了很多。

我的简单测试,同样一个算法,用C来写比用php效率能提高500倍。而用php扩展的方式,也能提高90多倍(其中的性能损失在了参数传递上了吧,我猜)。

所以有些时候php扩展就是我们的最佳选择了。

这里我着重介绍一下用C写php扩展的方法,而且不需要重新编译php。

首先,找到一个php的源码,php4或者php5版本的都可以,与你目标平台的php版本没有关系。

在源码的ext目录下可以找到名为ext_skel的脚本(windows平台使用ext_skel_win32.php)

在这个目录下执行./ext_skel --extname=hello(我用hello作为例子)

这时生成了一个目录 hello,目录下有几个文件,你只需要关心这三个:config.m4 hello.c php_hello.h

把这个目录拷备到任何你希望的地方,cd进去,依次执行

phpize

/configure

make

什么也没发生,对吧?

这是因为漏了一步,打开config.m4,找到下面

dnl If your extension references something external, use with:

dnl Otherwise use enable:

这是让你选择你的扩展使用with还是enable,我们用with吧。把with那一部分取消注释。

如果你和我一样使用vim编辑器,你就会很容易发现dnl三个字母原来是表示注释的呀(这是因为vim默认带了各种文件格式的语法着色包)

我们修改了config.m4后,继续

phpize

/configure

make

这时,modules下面会生成hello.so和hello.la文件。一个是动态库,一个是静态库。

你的php扩展已经做好了,尽管它还没有实现你要的功能,我先说说怎么使用这个扩展吧!ext_skel为你生成了一个hello.php里面有调用示例,但是那个例子需要你把hello.so拷贝到php的扩展目录中去,我们只想实现自己的功能,不想打造山寨版php,改用我下面的方法来加载吧:

  1. if(!extension_loaded("hello")) { 
  2.         dl_local("hello.so"); 
  3. function dl_local( $extensionFile ) { 
  4.         //make sure that we are ABLE to load libraries06.        if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) { 
  5.                 die"dh_local(): Loading extensions is not permitted./n" ); 
  6.         } //phpfensi.com 
  7.         //check to make sure the file exists11.        if( !file_exists(dirname(__FILE__) . "/". $extensionFile ) ) { 
  8.                 die"dl_local(): File '$extensionFile' does not exist./n" ); 
  9.         } 
  10.         //check the file permissions16.        if( !is_executable(dirname(__FILE__) . "/". $extensionFile ) ) { 
  11.                 die"dl_local(): File '$extensionFile' is not executable./n" ); 
  12.         } 
  13.         //we figure out the path21.        $currentDir = dirname(__FILE__) . "/"; 
  14.         $currentExtPath = ini_get"extension_dir" ); 
  15.         $subDirs = preg_match_all( "////" , $currentExtPath , $matches ); 
  16.         unset( $matches ); 
  17.         //lets make sure we extracted a valid extension path27.        if( !(bool)$subDirs ) { 
  18.                 die"dl_local(): Could not determine a valid extension path [extension_dir]./n" ); 
  19.         } 
  20.         $extPathLastChar = strlen$currentExtPath ) - 1; 
  21.         if$extPathLastChar == strrpos$currentExtPath , "/" ) ) { 
  22.                 $subDirs--; 
  23.         } 
  24.         $backDirStr = "";  
  25.         for$i = 1; $i <= $subDirs$i++ ) { 
  26.                 $backDirStr .= ".."
  27.                 if$i != $subDirs ) { 
  28.                   $backDirStr .= "/"
  29.                 } 
  30.         } 
  31.         //construct the final path to load46.        $finalExtPath = $backDirStr . $currentDir . $extensionFile; 
  32.         //now we execute dl() to actually load the module49.        if( !dl( $finalExtPath ) ) { 
  33.                 die(); 
  34.         } 
  35.         //if the module was loaded correctly, we must bow grab the module name54.        $loadedExtensions = get_loaded_extensions(); 
  36.         $thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ]; 
  37.         //lastly, we return the extension name58.        return $thisExtName; 
  38. }//end dl_local() 

这样的好处是你的php扩展可以随你的php代码走,绿色扩展。

随后一个让人关心的问题是,如何添加函数、实现参数传递和返回值

添加函数步骤如下:

  1. php_hello.h: 
  2. PHP_FUNCTION(confirm_hello_compiled);// 括号里面填写函数名 
  3. hello.c 
  4. zend_function_entry hello_functions[] = { 
  5.     PHP_FE(confirm_hello_compiled,  NULL)       /* 这里添加一行 */ 
  6.     {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */ 
  7. }; 
  8. PHP_FUNCTION(confirm_hello_compiled) 
    {// 这里写函数体

    }

要实现的函数原型其实都一个样,用宏PHP_FUNCTION来包装了一下,另外呢,在hello_functions里面添加了一行信息,表示你这个模块中有这个函数了。

那么都是一样的函数原型,如何区分返回值与参数呢?

我给一个例子,代码如下:

  1. PHP_FUNCTION(hello_strdiff) 
  2.     char *r1 = NULL, *r2 = NULL; 
  3.     int n = 0, m = 0; 
  4.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m) == FAILURE) { 
  5.         return
  6.     } 
  7.     while(n && m && *r1 == *r2) { 
  8.         r1++; 
  9.         r2++; 
  10.         n--; 
  11.         m--; 
  12.     } 
  13.     if(n == 0) RETURN_LONG(m); 
  14.     if(m == 0) RETURN_LONG(n); 
  15.     int d[n+1][m+1]; 
  16.     int cost; 
  17.     int i,j; 
  18.     for(i = 0; i <= n; i++) d[i][0] = i; 
  19.     for(j = 0; j <= m; j++) d[0][j] = j; 
  20.     for(i = 1; i <= n; i++) { 
  21.         for(j = 1; j <= m; j++) { 
  22.             if(r1[i-1] == r2[j-1]) cost = 0; 
  23.             else cost = 1; 
  24.             int a = MIN(d[i-1][j]+1,d[i][j-1]+1); 
  25.             a = MIN(a, d[i-1][j-1]+cost); 
  26.             d[i][j] = a; 
  27.         } 
  28.     } 
  29.     RETURN_LONG(d[n][m]); 

这是一个求两个字符串差异度的算法,输入参数两个字符串,返回整型。

参数的传递看这里

zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m)

把这个当成是scanf来理解好了。

类型说明见下表:

  1. Boolean b zend_bool 
  2. Long l long 
  3. Double d double 
  4. String s char*, int 
  5. Resource r zval* 
  6. Array a zval* 
  7. Object o zval* 
  8. zval z zval* 

如果想实现可选参数的话,例如一个字符串,一个浮点,再加一个可选的bool型,可以用"sd|b"来表示。

和scanf有一点不同的是,对于字符串,你要提供两个变量来存储,一个是char *,存字符串的地址,一个int,来存字符串的长度。这样有必要的时候,你可以安全的处理二进制数据。

那么返回值怎么办呢?

使用下面一组宏来表示:

  1. RETURN_STRING 
  2. RETURN_LONG 
  3. RETURN_DOUBLE 
  4. RETURN_BOOL 
  5. RETURN_NULL 

注意RETURN_STRING有两个参数

当你需要复制一份字符串时使用

RETURN_STRING("Hello World", 1);

否则使用

RETURN_STRING(str, 0);

这里涉及到了模块中内存的分配,当你申请的内存需要php程序中去释放的话,请参照如下表

  1. Traditional Non-Persistent Persistent 
  2. malloc(count
  3.            calloc(count, num) emalloc(count
  4.            ecalloc(count, num) pemalloc(count, 1)* 
  5.            pecalloc(count, num, 1) 
  6. strdup(str) 
  7.            strndup(str, len) estrdup(str) 
  8.            estrndup(str, len) pestrdup(str, 1) 
  9.            pemalloc() & memcpy() 
  10. free(ptr) efree(ptr) pefree(ptr, 1) 
  11. realloc(ptr, newsize) erealloc(ptr, newsize) perealloc(ptr, newsize, 1) 
  12. malloc(count * num + extr)** safe_emalloc(count, num, extr) safe_pemalloc(count, num, extr) 

一般我们使用Non-Persistent中列出的这些好了。

基本上就是这样,可以开始写一个php的扩展了。

从我目前的应用来看,能操纵字符串就够用了,所以我就只能介绍这么多了。



zend