zoukankan      html  css  js  c++  java
  • C扩展php的方法(制作php扩展库)

    用PHP调用C扩展整个配置过程在CentOS下
     
    今天终于把C扩展加入到PHP中了,并且可以调用,废话就不说了,看下文。
     
    一、必须先要安装Apache和mysql,这两个的安装过程我就不说了。
     
    二、安装PHP
     
    我是用的是YUM安装满装完了没有ext_skel,又下载了一个源码,
    #tar -zxvf php.xxx.tar.gz
     
    三、查看php是否安装成功
    在apache的htdocs里建立一个文件名为:index.php
    写入内容 <?phpinfo()?>
    保存
    浏览http://localhost/index.php
    显示正确,安装成功。
     
    四、创建自己的c php扩展
    进入到php的安装包的ext目录下
    #cd /home/eagle/soft/php/php-5.3.8/ext/
    #./ext_skel  --extname=caoqi                            注意:这里是创建里扩展库的名字,创建后,会在ext下有一个caoqi的目录
    #cd caoqi
     
     
    五、进行扩展库的基础修改和编码操作
    #vi config.m4
     
    原始
    dnl PHP_ARG_ENABLE(caoqi, whether to enable caoqi support,
    dnl Make sure that the comment is aligned:
    dnl [  --enable-caoqi           Enable caoqi support])
     
    修改后的
    PHP_ARG_ENABLE(caoqi, whether to enable caoqi support,
    Make sure that the comment is aligned:
    [  --enable-caoqi           Enable caoqi support])  #又说可以使用with的--with-caoqi ,我没有测试过
     
    #vi caoqi.c
     
    在PHP_FUNCTION(confirm_caoqi_compiled)函数下面追加
     
    PHP_FUNCTION(caoqi){
         zend_printf("helloword c!!");
    }
     
    修改前
    const zend_function_entry caoqi_functions[] = {
        PHP_FE(confirm_caoqi_compiled,    NULL)        
        {NULL, NULL, NULL}    
    };
     
    修改后
    const zend_function_entry caoqi_functions[] = {
        PHP_FE(confirm_caoqi_compiled,    NULL)        
        PHP_FE(caoqi,NULL)
        {NULL, NULL, NULL}    
    };
     
    #vi php_caoqi.h
    在PHP_FUNCTION(confirm_caoqi_compiled);下面追加
     
    PHP_FUNCTION(caoqi);
     
    到这里,基本上你的扩展库就写完了,下面就要开始进行生成和安装了
     
    五、生成扩展库
    当前目录在/usr/software/php5-3.2/ext/caoqi
     
    #/usr/bin/phpize  注意一定在你做的那个扩展目录下不然会报
    Cannot find config.m4. 
    Make sure that you run '/usr/bin/phpize' in the top level source directory of the module
    错误
     
    # ./configure --with-php-config=/usr/bin/php-config
     
    #make
    #make test
    #make install
    这时候会在 /usr/lib64/php/modules/
    下面出现caoqi.so 
     
    六、配置php.ini
    #vim /etc/php.d/caoqi.ini
    ; Enable gd extension module
    extension=caoqi.so
     
    重新启动apache
    service httpd restart
     
    然后
    vim /var/www/html/caoqi.php
     
    <?php
            echo caoqi();
    ?>
    在浏览器中打开出现
    helloword caoqi!!
     
    说明我已经成功了
     
     
     
    linux下php实现C/C++扩展编程
     
     
     
    某个功能被编译到so文件中,那么如何通过php来调用它?一个方法是写一个php模块(php extension),在php中调用该模块内的函数,再通过该模块来调用so中的函数。下面做一个简单的例子,使用的操作系统是Fedora Core 6。
     
     
     
     
        首先做一个简单的so文件:
     
       
         int hello_add(int a, int b)
         {
             return a + b;
         }
     
        然后将它编译成.so文件并放到系统中:
     
        $ gcc -O -c -fPIC -o hello.o hello.c
     
        $ gcc -shared -o libhello.so hello.o
     
        $ su
     
        # echo /usr/local/lib > /etc/ld.so.conf.d/local.conf
     
        # cp libhello.so /usr/local/lib
     
        # /sbin/ldconfig写段小程序来验证其正确性:
         
         #include <stdio.h>
         int main()
         {
             int a = 3, b = 4;
             printf("%d + %d = %d ", a, b, hello_add(a,b));
             return 0;
         }
     
        编译并执行:
     
        $ gcc -o hellotest -lhello hellotest.c
     
        $ ./hellotest
     
        3 + 4 = 7OK,
     
        下面我们来制作PHP模块。首先确保你安装了 php-devel 包,没有的话请自行从安装光盘上找。然后下载php源代码。我使用的是php-5.2.9.tar.gz,解压缩。
     
        $ tar xzvf php-5.2.9.tar.gz
     
        $ cd php-5.2.9/ext然后通过下面的命令建立一个名为 test 的模块。
     
        $ ./ext_skel --extname=test执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。如果要编译成可动态加载的 php_test.so,方法要更为简单。
     
        $ cd test首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)
     
        16:  PHP_ARG_ENABLE(test, whether to enable test support,
         17:  dnl Make sure that the comment is aligned:
         18:  [  --enable-test           Enable test support])
         然后执行 phpize(/usr/local/php5/bin/phpize)程序,生成configure脚本:
     
     $ phpize然后打开 php_test.h,在 PHP_FUNCTION(confirm_test_compiled); 之下加入函数声明:
     
        PHP_FUNCTION(confirm_hello_compiled);  
         PHP_FUNCTION(test_add);
         打开 test.c,在 PHP_FE(confirm_test_compiled, NULL) 下方加入以下内容。
         zend_function_entry test_functions[] = {
             PHP_FE(confirm_test_compiled,  NULL)      
             PHP_FE(test_add,   NULL)      
             {NULL, NULL, NULL}  
         };
     
        然后在 test.c 的最末尾书写test_add函数的内容:
     
        PHP_FUNCTION(test_add)
         {
             long int a, b;
             long int result;
             if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
                 return;
             }
             result = hello_add(a, b);
             RETURN_LONG(result);
         }
     
        保存退出,编译并安装:
     
        $ ./configure --with-php-config=/usr/local/php5/bin/php-config
        $ make LDFLAGS=-lhello //很重要
        $ make test
        $ make install
     
        # cp modules/test.so /usr/lib/php/modules然后在 /www/web/ 下建立一个 test.php 文件,内容如下:
     
        <?php
             //dl("test.so"); //这个不管用了,从php5.3以后就弃用了,可以用以下方法加载
             echo test_add(1, 2);
         ?>
         在php.ini配置文件中加入
         extension=test.so
         
         重启apache,然后测试test.php
  • 相关阅读:
    hihocoder1634 Puzzle Game
    hihocoder1580 Matrix
    BZOJ3036 绿豆蛙的归宿
    CF|codeforces 280C Game on Tree
    [SDOI2011] 计算器
    [SCOI2007] 修车
    [JSOI2008] 球形空间产生器sphere
    APIO2012 派遣dispatching | 左偏树
    OI数据结构&&分治 简单学习笔记
    BZOJ3307 雨天的尾巴
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8334645.html
Copyright © 2011-2022 走看看