Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3045561
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2017-05-27 11:44:29

PHP除了使用扩展库的方式调用c函数,还可以通过socket通信的方式。这里介绍前者。 

第一步: 环境搭建

1. 先看本机是否已经安装了较低版本的php
   #find /usr -name "php"
   或者rpm -aq | grep php
   如果存在,就使用rpm命令等方式卸掉
2. php源码安装,configure的参数如下
#./configure  --prefix=/usr/local/php   --with-mysql --with-mysqli  --enable-so  --with-pdo-mysq=/var/lib/mysql  --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --enable-zip --enable-mbstring
3. 如果已经安装了两个版本的php,使用apache函数,确定使用的是哪个版本的php





4. 如果已经安装了两个版本的php,使用php查看版本号或确定php.ini的配置文件 
#/usr/local/php/bin/php -v
#/usr/local/php/bin/php -i | grep php.ini

第二步: 用C生成so文件

#vim hello.c
int cc_add(int a, int b)
{
    return a + b;
}
# gcc -O -c -fPIC -o hello.o hello.c                      // -fPIC:是指生成的动态库与位置无关
# gcc -shared -o libhello.so hello.o                     // -shared:是指明生成动态链接库
# cp libhello.so /usr/local/lib      // 把生成的链接库放到指定的地址
# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf       //  把库地址写入到配置文件中
# /sbin/ldconfig                // 用此命令,使刚才写的配置文件生效
以下是测试so文件
#include 
int main()
{
    int a = 4, b = 6;
    printf("%d\n", cc_add(a,b));
    return 0;
}
#gcc -o hellotest -lhello hellotest.c                // 编译测试文件,生成测试程序
#./hellotest           // 运行测试程序

第三步: 制作PHP模块(外部模块)

1. 然后通过下面的命令用ext_skel脚本建立一个名为 hello 的模块:
#cd php-5.3.6/ext
#./ext_skel --extname=hello

2. 执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。
如果要编译成可动态加载的 php_hello.so,方法要更为简单。
#cd hello
首先编辑 config.m4 文件,去掉注释(注释符号为 dnl 。)
# vim config.m4
PHP_ARG_ENABLE(hello, whether to enable hello support,
dnl Make sure that the comment is aligned:
[  --enable-hello           Enable hello support])

3. 然后执行 phpize 程序,生成configure脚本:
#phpize

4. 打开 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函数声明:
PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */
PHP_FUNCTION(hello_add);

5. 打开 hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下内容。
zend_function_entry hello_functions[] = {
PHP_FE(confirm_hello_compiled,  NULL)       /* For testing, remove later. */
PHP_FE(hello_add,   NULL)       /* For testing, remove later. */
{NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */};

然后在 hello.c 的最末尾书写hello_add函数的内容:
PHP_FUNCTION(hello_add)
{
    long int a, b;
    long int result;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
        return;
    }
    result = cc_add(a, b);

    RETURN_LONG(result);
}
6.配置安装 
#./configure --with-php-config=/usr/local/php/bin/php-config 
#make LDFLAGS=-lhello
#make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
#cp modules/hello.so /usr/lib/php/modules

#vim /etc/php5/apache2/php.ini

enable_dl = Off; //允许dl()动态加载so扩展功能enable_dl = On

# /etc/init.d/httpd restart
然后在 /var/www/html 下建立一个 hello.php 文件,内容如下:




然后在浏览器中打开hello.php文件,如果显示10,则说明函数调用成功了。

第四步. 制作PHP模块(内部模块)

另外可以在apache重启的时候让我们的so库直接动态编译进php5,就像linux的insmod hello.ko模块一样,不用dl加载也不用重新编译php,就可以直接使用so的函数了,步骤如下:

# vim /etc/php5/apache2/php.ini
enable_dl = Off
extension=hello.so
# /etc/init.d/httpd restart

[注意,这种方式只适合hello.so库内所有功能代码已经全部调试ok,如果还处在调试期间,那么需要采用上面的dl强制加载的方式]

代码如下:





阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~