0

0

C语言PHP的helloworld扩展

php中文网

php中文网

发布时间:2016-06-23 13:31:27

|

1213人浏览过

|

来源于php中文网

原创

1、下载php源码

wget http://cn2.php.net/distributions/php-5.6.10.tar.gztar -zxvf php-5.6.10.tar.gz

2、建立扩展开发框架 ./ext_skel --extname=helloworld

cd php-5.6.10/ext/./ext_skel --extname=helloworldCreating directory helloworldCreating basic files: config.m4 config.w32 .gitignore helloworld.c php_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt helloworld.php [done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/helloworld/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-helloworld5.  $ make6.  $ ./sapi/cli/php -f ext/helloworld/helloworld.php7.  $ vi ext/helloworld/helloworld.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/helloworld/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

3、进入php源码的根目录, 编辑文件 vim ext/helloworld/config.m4

去掉这几行代码前面的dnl
PHP_ARG_ENABLE(helloworld, whether to enable helloworld support,Make sure that the comment is aligned:[  --enable-helloworld           Enable helloworld support])

4、在php源码根目录执行命令 ./buildconf --force

立即学习PHP免费学习笔记(深入)”;

Forcing buildconfRemoving configure cachesbuildconf: checking installation...buildconf: autoconf version 2.69 (ok)rebuilding configurerebuilding main/php_config.h.in

5、在php源码的根目录编译php程序,注意命令为 ./configure --with-helloworld

configure: WARNING: unrecognized options: --with-helloworldchecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for a sed that does not truncate output... /bin/sedchecking build system type... x86_64-unknown-linux-gnuchecking host system type... x86_64-unknown-linux-gnuchecking target system type... x86_64-unknown-linux-gnuchecking for cc... ccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ISO C89... none neededchecking how to run the C preprocessor... cc -Echecking for icc... nochecking for suncc... no......最后出现的error,没有处理configure: error: xml2-config not found. Please check your libxml2 installation.

6、进入我们的扩展目录helloworld,执行命令 phpize(通过 sudo apt-get install php5-dev 安装 phpize),此时你的扩展目录会生成很多文件,可以用于后期编译。

cd php-5.6.10/ext/helloworldphpizeConfiguring for:PHP Api Version:         20121113Zend Module Api No:      20121212Zend Extension Api No:   220121212

7、在helloworld目录编译我们的扩展 ./configure --with-php-config=/usr/bin/php-config(使用你自己环境的php-config) --enable-helloworld

     你可以通过一条命令查找你的php-config文件的位置(find / -name php-config)如下:我的地址为/usr/bin/php-config

./configure --with-php-config=/usr/bin/php-configchecking for grep that handles long lines and -e... /bin/grepchecking for egrep... /bin/grep -Echecking for a sed that does not truncate output... /bin/sedchecking for cc... ccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ISO C89... none neededchecking how to run the C preprocessor... cc -Echecking for icc... nochecking for suncc... nochecking whether cc understands -c and -o together... yeschecking for system library directory... libchecking if compiler supports -R... no......

8、进入扩展helloworld目录,编辑文件php_helloworld.h,在最后一行添加函数PHP_FUNCTION(helloworldTest);

PHP_FUNCTION(helloworldTest);

9、用vim 打开helloword.c,在helloworld.c中实现我们的函数,之后 将helloworldTest函数加入到helloworld_functions[]中,保存退出

PHP_FUNCTION(helloworldTest){        RETURN_STRING("Hello World !", 1);}const zend_function_entry helloworld_functions[] = {        PHP_FE(confirm_helloworld_compiled,     NULL)           /* For testing, remove later. */        PHP_FE(helloworldTest, NULL)        PHP_FE_END      /* Must be the last line in helloworld_functions[] */};

10、执行make命令 make 编译扩展,我在运行的过程中还是比较顺利的。如果出现错误,请认真看前面步骤是否有错,我在第一遍做的时候也有错一般都是前面步骤有问题。

DBShop开源商城系统
DBShop开源商城系统

DBShop开源商城系统,使用PHP语言基于Laminas(Zendframework 3) + Doctrine 2 组合框架开发完成。可定制、多终端、多场景、多支付、多货币;严谨的安全机制,可靠稳定;方便的操作管理,节约时间;清晰的权限分配,责任分明;便捷的更新处理,一键搞定;丰富的插件市场,扩展无限。

下载

/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=compile cc  -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /php/php-5.6.10/ext/helloworld/helloworld.c -o helloworld.lo libtool: compile:  cc -I. -I/php/php-5.6.10/ext/helloworld -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /php/php-5.6.10/ext/helloworld/helloworld.c  -fPIC -DPIC -o .libs/helloworld.o/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=link cc -DPHP_ATOM_INC -I/php/php-5.6.10/ext/helloworld/include -I/php/php-5.6.10/ext/helloworld/main -I/php/php-5.6.10/ext/helloworld -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -o helloworld.la -export-dynamic -avoid-version -prefer-pic -module -rpath /php/php-5.6.10/ext/helloworld/modules  helloworld.lo libtool: link: cc -shared  -fPIC -DPIC  .libs/helloworld.o    -O2   -Wl,-soname -Wl,helloworld.so -o .libs/helloworld.solibtool: link: ( cd ".libs" && rm -f "helloworld.la" && ln -s "../helloworld.la" "helloworld.la" )/bin/bash /php/php-5.6.10/ext/helloworld/libtool --mode=install cp ./helloworld.la /php/php-5.6.10/ext/helloworld/moduleslibtool: install: cp ./.libs/helloworld.so /php/php-5.6.10/ext/helloworld/modules/helloworld.solibtool: install: cp ./.libs/helloworld.lai /php/php-5.6.10/ext/helloworld/modules/helloworld.lalibtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/sbin" ldconfig -n /php/php-5.6.10/ext/helloworld/modules----------------------------------------------------------------------Libraries have been installed in:   /php/php-5.6.10/ext/helloworld/modulesIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify the full pathname of the library, or use the `-LLIBDIR'flag during linking and do at least one of the following:   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable     during execution   - add LIBDIR to the `LD_RUN_PATH' environment variable     during linking   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag   - have your system administrator add LIBDIR to `/etc/ld.so.conf'See any operating system documentation about shared libraries formore information, such as the ld(1) and ld.so(8) manual pages.----------------------------------------------------------------------Build complete.Don't forget to run 'make test'.

11、安装php

apt-get install php5 php5-gd php5-cli

11、将编译生成的helloworld.so 文件复制到你本机的php扩展目录

创建:info.php内容:phpinfo();php info.php | grep extenextension_dir => /usr/lib/php5/20121212 => /usr/lib/php5/20121212mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.root@4ccdc77255ea:/php/php-5.6.10/ext/helloworld# cp modules/helloworld.so /usr/lib/php5/20121212/

12、配置php.ini 开启 helloworld.so 扩展

php info.php | grep php.iniLoaded Configuration File => /etc/php5/cli/php.iniroot@4ccdc77255ea:/php# vim /etc/php5/cli/conf.d/  php.ini

13、创建echo.php

  

14、Docker中遇到的问题

1. apt-get install autoconf2. gcc make等依赖包安装









相关文章

C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

178

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

35

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

79

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

2

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

4

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

8

2026.01.28

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

24

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

122

2026.01.26

edge浏览器怎样设置主页 edge浏览器自定义设置教程
edge浏览器怎样设置主页 edge浏览器自定义设置教程

在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

72

2026.01.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C++教程
C++教程

共115课时 | 14.5万人学习

XML教程
XML教程

共142课时 | 5.9万人学习

php-src源码分析探索
php-src源码分析探索

共6课时 | 0.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号