PHP调用C语言实现接口方法

环境:

CentOS Linux release 7.3.1611 (Core)

PHP 5.4.16

安装php、查看php版本

# yum install php php-devel

# php -v

[root@localhost hello]# cat hello.c

#include <stdio.h>

int hello_add (int a, int b)

{

return a+b;

}

生成动态库:

# gcc -fPIC -shared -o libhello.so hello.c

# cp libhello.so /usr/local/lib // 把生成的链接库放到指定的地址  

# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf //把库地址写入到配置文件中  

# ldconfig // 用此命令,使刚才写的配置文件生效  

对so文件进行测。

[root@localhost hello]# cat hello-test.c

#include <stdio.h>

int main()

{

int a = 4, b = 6;

printf("%d\n", hello_add(a,b));

return 0;

}

# gcc hello-test.c

# ./a.out

1. 下载与系统安装的同一版本的php源码, 这里选择php 5.4.16版本:

PHP: Releases

2. 进入php-5.4.16/ext/ 目录下,使用ext_skel生成扩展骨架: 即生成名为hello的模块。

# cd php-5.4.16/ext

# ./ext_skel --extname=hello

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值