开发的第一个PHP扩展
下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录
wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror
tar -xzvf php-5.4.23.tar.gz
cd php-5.4.23/ext/
在ext目录中新建config.m4文件
PHP_ARG_ENABLE(heiyoubo,
[Whether to enable the "heiyoubo" extension],
[ enable-heiyoubo Enable "heiyoubo" extension support]) if test $PHP_HEIYOUBO != "no"; then
PHP_SUBST(HEIYOUBO_SHARED_LIBADD)
PHP_NEW_EXTENSION(heiyoubo, heiyoubo.c, $ext_shared)
fi
在ext目录中新建heiyoubo.c文件
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif //加载php头文件
#include "php.h" #define phpext_heiyoubo_ptr &heiyoubo_module_entry ZEND_FUNCTION(heiyoubo_hello)
{
php_printf("Hello World Heiyoubo!\n");
} static zend_function_entry heiyoubo_functions[] = {
ZEND_FE(heiyoubo_hello, NULL)
{ NULL, NULL, NULL }
}; //module entry
zend_module_entry heiyoubo_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"heiyoubo", //这个地方是扩展名称,往往我们会在这个地方使用一个宏。
heiyoubo_functions, /* Functions */
NULL, /* MINIT */
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
"2.1", //这个地方是我们扩展的版本
#endif
STANDARD_MODULE_PROPERTIES
}; #ifdef COMPILE_DL_HEIYOUBO
ZEND_GET_MODULE(heiyoubo)
#endif
运行phpize,准备 PHP 扩展库的编译环境
[hubo@test15169x ~/php-5.4.23/ext]$ /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
configure的时候要开启heiyoubo扩展,并且指定php-config的目录,获取所安装的 PHP 配置的信息
[hubo@test15169x ~/php-5.4.23/ext]$ ./configure --enable-heiyoubo --with-php-config=/usr/local/php/bin/php-config
[hubo@test15169x ~/php-5.4.23/ext]$ make
[hubo@test15169x ~/php-5.4.23/ext]$ make test
heiyoubo.so扩展已经生成到module目录
[hubo@test15169x ~/php-5.4.23/ext]$ ll modules/*
-rw-rw-r-- 1 hubo hubo 799 01-09 16:53 modules/heiyoubo.la
-rwxrwxr-x 1 hubo hubo 26K 01-09 16:53 modules/heiyoubo.so
将heiyoubo.so文件拷贝到php的扩展目录
[hubo@test15169x ~/php-5.4.23/ext]$ php -ini | grep extension_dir
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
[hubo@test15169x ~/php-5.4.23/ext]$ cp modules/heiyoubo.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
将heiyoubo.so加到php的扩展文件/usr/local/etc/cgi/php.ini中 extension = "heiyoubo.so"
[hubo@test15169x ~/php-5.4.23/ext]$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/cgi
Loaded Configuration File: /usr/local/etc/cgi/php.ini
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'var_dump(get_loaded_extensions());' | grep heiyoubo
string(8) "heiyoubo"
验证安装成功。执行C扩展中函数heiyoubo_hello();执行成功
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'heiyoubo_hello();'
Hello World Heiyoubo!
参考链接:
http://www.php.net/manual/zh/internals2.buildsys.configunix.php
https://github.com/walu/phpbook/blob/master/5.1.md 《Extending and Embedding PHP》中文版翻译 PHP扩展开发及内核应用
http://www.laruence.com/2009/04/28/719.html
开发的第一个PHP扩展的更多相关文章
- PHP扩展开发--01.编写一个helloWorld扩展
为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍. 另外C扩展是在进程启动时加载的,PHP代码只能操作Request生命周期的数据 ...
- 如何用原生js开发一个Chrome扩展程序
原文地址:How to Build a Simple Chrome Extension in Vanilla JavaScript 开发一个Chrome扩展程序非常简单,只需要使用原生的js就可以完成 ...
- 快速开发一个PHP扩展
快速开发一个PHP扩展 作者:heiyeluren时间:2008-12-5博客:http://blog.csdn.net/heiyeshuwu 本文通过非常快速的方式讲解了如何制作一个PHP 5.2 ...
- 如何基于 PHP-X 快速开发一个 PHP 扩展
0x01 起步 PHP-X本身基于C++11开发,使用cmake进行编译配置.首先,你需要确定所有依赖项已安装好.包括: gcc-4.8 或更高版本 PHP7.0 或更高版本,需要php7-dev 开 ...
- [php-src]一个Php扩展的结构
内容均以php5.6.14为例. 要拥有一个PHP扩展的架子,使用源码中准备好的 /ext/ext_skel 工具,可以生成一个可运行的扩展骨架. 不加选项运行 ./ext_skel,可查看所有可用选 ...
- 【视频】从零开始编写第一个PHP扩展
Rango会讲解在Linux下从零开始写一个PHP扩展,并编译安装到PHP里,一直到执行扩展中的函数.包含的内容有: 为什么要开发PHP扩展 ext_skel工具的使用 修改config.m4 php ...
- 我的第一个chrome扩展(1)——读样例,实现时钟
学习chrome扩展开发: 与网页类似,需要的知识:html,javascript chrome扩展程序的构成: manifest.json:对扩展程序的整体描述文件 { "manifest ...
- 项目开发中封装一个BarButtonItem类别-很实用
Encapsulates a TabBarItem--封装一个BarButtonItem类 在我们程序的导航栏的左边或右边一般都会有这样的BarButtonItem,用来界面之间的跳转 如果我们有很多 ...
- 提高 JavaScript 开发效率的高级 VSCode 扩展!
原文:提高 JavaScript 开发效率的高级 VSCode 扩展! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. Quokka.js Quokka.js 是一个用于 JavaSc ...
随机推荐
- Android Retrofit使用教程
Square公司开源了许多优秀的库,Retrofit就是其中之一. Retrofit是用来简化APP访问服务器API,如果你的服务器使用的使RESTAPI,那么赶紧使用Retrofit吧. 官方的文档 ...
- Working With Push Buttons In Oracle Forms
Managing push buttons at run time in Oracle Forms is very simple and in this tutorial you will learn ...
- IntelliJ IDEA 识别一个类所属的jar包package
IntelliJ IDEA 识别一个类所属的jar包package 按住ctrl,鼠标移动上去,不要点击: 有木有快捷键? ctrl+alt+B直接就过去了:需要再跳回来:
- Log4cplus入门
Log4cplus使用指南 1. Log4cplus简单介绍 log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统.受Apache Software License保护 ...
- 0x…… is not a valid instance ID怎么解决
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- beforeRouteLeave 实现vue路由拦截浏览器的需求,进行一系列操作 草稿保存等等
场景:为了防止用户失误点错关闭按钮等等,导致没有保存已输入的信息(关键信息).用法://在路由组件中: beforeRouteLeave (to, from, next) { if(用户已经输入信息) ...
- js:string转int
http://blog.csdn.net/leidengyan/article/details/5503594 <script> var str='1250' ; aler ...
- UML的基本图(三)
An artifact diagram shows the physical constituents of a system on the computer. Artifacts includ ...
- 在VS2013中打开Nuget
1.工具→NuGet程序包管理器→程序包管理控制台 2.没有的话,就去 工具→扩展和更新 搜索nuget
- NodeJS 安装cnpm命令行工具
在安装之前,请确保已安装Git和NodeJS. cmd机内命令窗口,输入以下命令: git config --system http.sslcainfo /bin/curl-ca-bundle.crt ...