当前位置:   article > 正文

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

php调用c语言

环境:

CentOS Linux release 7.3.1611 (Core)

PHP 5.4.16

安装php、查看php版本

# yum install php php-devel

# php -v

  1. [root@localhost hello]# cat hello.c
  2. #include <stdio.h>
  3. int hello_add (int a, int b)
  4. {
  5. return a+b;
  6. }

生成动态库:

# 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文件进行测。

  1. [root@localhost hello]# cat hello-test.c
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int a = 4, b = 6;
  6. printf("%d\n", hello_add(a,b));
  7. return 0;
  8. }

# 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/50951
推荐阅读
相关标签
  

闽ICP备14008679号