示例 PHP开发编译自己的拓展_宝塔php编译模块-程序员宅基地

技术标签: php  php拓展  宝塔编译php拓展  

环境

centos+宝塔+php7.2

步骤

一:生成扩展目录
PHP源码扩展目录ext/中包含生成扩展基本目录的工具ext_skel,使用该工具可以很方便的生成扩展基本目录。
宝塔位置为:/www/server/php/72/src/ext/

#cd /www/server/php/72/src/ext/
//my_test_ext 为拓展名字
#./ext_skel --extname=my_test_ext

完成后会自动创建 my_test_ext 文件夹
进入文件夹

cd my_test_ext

二:修改 config.m4
这里dnl 开头为注释的意思

这里先注释16-18行,应该是静态编译需要的参数吧,还不太清楚

dnl $Id$
dnl config.m4 for extension my_test_ext

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(my_test_ext, for my_test_ext support,
dnl Make sure that the comment is aligned:
dnl [  --with-my_test_ext             Include my_test_ext support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(my_test_ext, whether to enable my_test_ext support,
 Make sure that the comment is aligned:
[  --enable-my_test_ext           Enable my_test_ext support])

if test "$PHP_MY_TEST_EXT" != "no"; then
  dnl Write more examples of tests here...

  dnl # get library FOO build options from pkg-config output
  dnl AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
  dnl AC_MSG_CHECKING(for libfoo)
  dnl if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists foo; then
  dnl   if $PKG_CONFIG foo --atleast-version 1.2.3; then
  dnl     LIBFOO_CFLAGS=`$PKG_CONFIG foo --cflags`
  dnl     LIBFOO_LIBDIR=`$PKG_CONFIG foo --libs`
  dnl     LIBFOO_VERSON=`$PKG_CONFIG foo --modversion`
  dnl     AC_MSG_RESULT(from pkgconfig: version $LIBFOO_VERSON)
  dnl   else
  dnl     AC_MSG_ERROR(system libfoo is too old: version 1.2.3 required)
  dnl   fi
  dnl else
  dnl   AC_MSG_ERROR(pkg-config not found)
  dnl fi
  dnl PHP_EVAL_LIBLINE($LIBFOO_LIBDIR, MY_TEST_EXT_SHARED_LIBADD)
  dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS)

  dnl # --with-my_test_ext -> check with-path
  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
  dnl SEARCH_FOR="/include/my_test_ext.h"  # you most likely want to change this
  dnl if test -r $PHP_MY_TEST_EXT/$SEARCH_FOR; then # path given as parameter
  dnl   MY_TEST_EXT_DIR=$PHP_MY_TEST_EXT
  dnl else # search default path list
  dnl   AC_MSG_CHECKING([for my_test_ext files in default path])
  dnl   for i in $SEARCH_PATH ; do
  dnl     if test -r $i/$SEARCH_FOR; then
  dnl       MY_TEST_EXT_DIR=$i
  dnl       AC_MSG_RESULT(found in $i)
  dnl     fi
  dnl   done
  dnl fi
  dnl
  dnl if test -z "$MY_TEST_EXT_DIR"; then
  dnl   AC_MSG_RESULT([not found])
  dnl   AC_MSG_ERROR([Please reinstall the my_test_ext distribution])
  dnl fi

  dnl # --with-my_test_ext -> add include path
  dnl PHP_ADD_INCLUDE($MY_TEST_EXT_DIR/include)

  dnl # --with-my_test_ext -> check for lib and symbol presence
  dnl LIBNAME=my_test_ext # you may want to change this
  dnl LIBSYMBOL=my_test_ext # you most likely want to change this 

  dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  dnl [
  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MY_TEST_EXT_DIR/$PHP_LIBDIR, MY_TEST_EXT_SHARED_LIBADD)
  dnl   AC_DEFINE(HAVE_MY_TEST_EXTLIB,1,[ ])
  dnl ],[
  dnl   AC_MSG_ERROR([wrong my_test_ext lib version or lib not found])
  dnl ],[
  dnl   -L$MY_TEST_EXT_DIR/$PHP_LIBDIR -lm
  dnl ])
  dnl
  dnl PHP_SUBST(MY_TEST_EXT_SHARED_LIBADD)

  PHP_NEW_EXTENSION(my_test_ext, my_test_ext.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi

这里关键是 最后一行 PHP_NEW_EXTENSION 这个方法
这里会加载你写的C代码,现在默认加载了my_test_ext.c,如果有多个应该这样加

dnl 多个C文件中间用空格键隔开
PHP_NEW_EXTENSION(my_test_ext, my_test_ext.c test_2.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

三:实现自己的拓展方法
暂时只有一个C文件 我们就在默认生成的my_test_ext.c 里面添加自己的方法


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_my_test_ext.h"

/* If you declare any globals in php_my_test_ext.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(my_test_ext)
*/

/* True global resources - no need for thread safety here */
static int le_my_test_ext;

/* {
   {
   { PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("my_test_ext.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_my_test_ext_globals, my_test_ext_globals)
    STD_PHP_INI_ENTRY("my_test_ext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_my_test_ext_globals, my_test_ext_globals)
PHP_INI_END()
*/
/* }}} */

/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {
   {
   { proto string confirm_my_test_ext_compiled(string arg)
   Return a string to confirm that the module is compiled in */
// PHP_FUNCTION(confirm_my_test_ext_compiled)
// {
// 	char *arg = NULL;
// 	size_t arg_len, len;
// 	zend_string *strg;

// 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
// 		return;
// 	}

// 	strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "my_test_ext", arg);

// 	RETURN_STR(strg);
// }
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   follow this convention for the convenience of others editing your code.
*/


/* {
   {
   { php_my_test_ext_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_my_test_ext_init_globals(zend_my_test_ext_globals *my_test_ext_globals)
{
	my_test_ext_globals->global_value = 0;
	my_test_ext_globals->global_string = NULL;
}
*/
/* }}} */

/* {
   {
   { PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(my_test_ext)
{
	/* If you have INI entries, uncomment these lines
	REGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */

/* {
   {
   { PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(my_test_ext)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {
   {
   { PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(my_test_ext)
{
#if defined(COMPILE_DL_MY_TEST_EXT) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {
   {
   { PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(my_test_ext)
{
	return SUCCESS;
}
/* }}} */

/* {
   {
   { PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(my_test_ext)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "my_test_ext support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}

//定义拓展方法
ZEND_FUNCTION(sey_hello)
{
    php_printf("Hello World!\n");
}

/* }}} */

/* {
   {
   { my_test_ext_functions[]
 *这里需要添加自己拓展的方法
 * Every user visible function must have an entry in my_test_ext_functions[].
 */
const zend_function_entry my_test_ext_functions[] = {
	// PHP_FE(confirm_my_test_ext_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(tl_get_arch, NULL)
	PHP_FE_END	/* Must be the last line in my_test_ext_functions[] */
};
/* }}} */

/* {
   {
   { my_test_ext_module_entry
 */
zend_module_entry my_test_ext_module_entry = {
	STANDARD_MODULE_HEADER,
	"my_test_ext",
	my_test_ext_functions,
	PHP_MINIT(my_test_ext),
	PHP_MSHUTDOWN(my_test_ext),
	PHP_RINIT(my_test_ext),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(my_test_ext),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(my_test_ext),
	PHP_MY_TEST_EXT_VERSION,
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_MY_TEST_EXT
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(my_test_ext)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */

里面125 行添加了自定义的方法 say_hello

四:把方法添加到PHP拓展调用层
my_test_ext.c 里修改

const zend_function_entry my_test_ext_functions[] = {
	// PHP_FE(confirm_my_test_ext_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(say_hello, NULL)
	PHP_FE_END	/* Must be the last line in my_test_ext_functions[] */
};

五:在php_my_test_ext.h 里添加方法声明


#ifndef PHP_MY_TEST_EXT_H
#define PHP_MY_TEST_EXT_H

extern zend_module_entry my_test_ext_module_entry;
#define phpext_my_test_ext_ptr &my_test_ext_module_entry

#define PHP_MY_TEST_EXT_VERSION "0.1.0" /* Replace with version number for your extension */

#ifdef PHP_WIN32
#	define PHP_MY_TEST_EXT_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#	define PHP_MY_TEST_EXT_API __attribute__ ((visibility("default")))
#else
#	define PHP_MY_TEST_EXT_API
#endif

#ifdef ZTS
#include "TSRM.h"
#endif

/*
  	Declare any global variables you may need between the BEGIN
	and END macros here:

ZEND_BEGIN_MODULE_GLOBALS(my_test_ext)
	zend_long  global_value;
	char *global_string;
ZEND_END_MODULE_GLOBALS(my_test_ext)
*/

/* Always refer to the globals in your function as MY_TEST_EXT_G(variable).
   You are encouraged to rename these macros something shorter, see
   examples in any other php module directory.
*/
#define MY_TEST_EXT_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(my_test_ext, v)

#if defined(ZTS) && defined(COMPILE_DL_MY_TEST_EXT)
ZEND_TSRMLS_CACHE_EXTERN()
#endif
PHP_FUNCTION(say_hello);
#endif	/* PHP_MY_TEST_EXT_H */

倒数第二行 添加的方法声明

六:编译拓展
拓展文件内

#make clean
#/www/server/php/72/bin/phpize
#./configure --with-php-config=/www/server/php/72/bin/php-config
#make && make install

逐条执行完上面的指令则编译好自定义拓展了
最后在php.ini 加上拓展 重启php 则可以调用了
extension= /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/my_test_ext.so

最后 php调用

   //测试自己的拓展
    public function test(){
        say_hello();
    }

参考资料:php内核开发
https://github.com/walu/phpbook

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lqb3732842/article/details/123827655

智能推荐

解决win10/win8/8.1 64位操作系统MT65xx preloader线刷驱动无法安装_mt65驱动-程序员宅基地

文章浏览阅读1.3w次。转载自 http://www.miui.com/thread-2003672-1-1.html 当手机在刷错包或者误修改删除系统文件后会出现无法开机或者是移动定制(联通合约机)版想刷标准版,这时就会用到线刷,首先就是安装线刷驱动。 在XP和win7上线刷是比较方便的,用那个驱动自动安装版,直接就可以安装好,完成线刷。不过现在也有好多机友换成了win8/8.1系统,再使用这个_mt65驱动

SonarQube简介及客户端集成_sonar的客户端区别-程序员宅基地

文章浏览阅读1k次。SonarQube是一个代码质量管理平台,可以扫描监测代码并给出质量评价及修改建议,通过插件机制支持25+中开发语言,可以很容易与gradle\maven\jenkins等工具进行集成,是非常流行的代码质量管控平台。通CheckStyle、findbugs等工具定位不同,SonarQube定位于平台,有完善的管理机制及强大的管理页面,并通过插件支持checkstyle及findbugs等既有的流..._sonar的客户端区别

元学习系列(六):神经图灵机详细分析_神经图灵机方法改进-程序员宅基地

文章浏览阅读3.4k次,点赞2次,收藏27次。神经图灵机是LSTM、GRU的改进版本,本质上依然包含一个外部记忆结构、可对记忆进行读写操作,主要针对读写操作进行了改进,或者说提出了一种新的读写操作思路。神经图灵机之所以叫这个名字是因为它通过深度学习模型模拟了图灵机,但是我觉得如果先去介绍图灵机的概念,就会搞得很混乱,所以这里主要从神经图灵机改进了LSTM的哪些方面入手进行讲解,同时,由于模型的结构比较复杂,为了让思路更清晰,这次也会分开几..._神经图灵机方法改进

【机器学习】机器学习模型迭代方法(Python)-程序员宅基地

文章浏览阅读2.8k次。一、模型迭代方法机器学习模型在实际应用的场景,通常要根据新增的数据下进行模型的迭代,常见的模型迭代方法有以下几种:1、全量数据重新训练一个模型,直接合并历史训练数据与新增的数据,模型直接离线学习全量数据,学习得到一个全新的模型。优缺点:这也是实际最为常见的模型迭代方式,通常模型效果也是最好的,但这样模型迭代比较耗时,资源耗费比较多,实时性较差,特别是在大数据场景更为困难;2、模型融合的方法,将旧模..._模型迭代

base64图片打成Zip包上传,以及服务端解压的简单实现_base64可以装换zip吗-程序员宅基地

文章浏览阅读2.3k次。1、前言上传图片一般采用异步上传的方式,但是异步上传带来不好的地方,就如果图片有改变或者删除,图片服务器端就会造成浪费。所以有时候就会和参数同步提交。笔者喜欢base64图片一起上传,但是图片过多时就会出现数据丢失等异常。因为tomcat的post请求默认是2M的长度限制。2、解决办法有两种:① 修改tomcat的servel.xml的配置文件,设置 maxPostSize=..._base64可以装换zip吗

Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字-程序员宅基地

文章浏览阅读1k次,点赞17次,收藏22次。Opencv自然场景文本识别系统(源码&教程)_opencv自然场景实时识别文字

随便推点

ESXi 快速复制虚拟机脚本_exsi6.7快速克隆centos-程序员宅基地

文章浏览阅读1.3k次。拷贝虚拟机文件时间比较长,因为虚拟机 flat 文件很大,所以要等。脚本完成后,以复制虚拟机文件夹。将以下脚本内容写入文件。_exsi6.7快速克隆centos

好友推荐—基于关系的java和spark代码实现_本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。-程序员宅基地

文章浏览阅读2k次。本文主要实现基于二度好友的推荐。数学公式参考于:http://blog.csdn.net/qq_14950717/article/details/52197565测试数据为自己随手画的关系图把图片整理成文本信息如下:a b c d e f yb c a f gc a b dd c a e h q re f h d af e a b gg h f bh e g i di j m n ..._本关任务:使用 spark core 知识完成 " 好友推荐 " 的程序。

南京大学-高级程序设计复习总结_南京大学高级程序设计-程序员宅基地

文章浏览阅读367次。南京大学高级程序设计期末复习总结,c++面向对象编程_南京大学高级程序设计

4.朴素贝叶斯分类器实现-matlab_朴素贝叶斯 matlab训练和测试输出-程序员宅基地

文章浏览阅读3.1k次,点赞2次,收藏12次。实现朴素贝叶斯分类器,并且根据李航《统计机器学习》第四章提供的数据训练与测试,结果与书中一致分别实现了朴素贝叶斯以及带有laplace平滑的朴素贝叶斯%书中例题实现朴素贝叶斯%特征1的取值集合A1=[1;2;3];%特征2的取值集合A2=[4;5;6];%S M LAValues={A1;A2};%Y的取值集合YValue=[-1;1];%数据集和T=[ 1,4,-1;..._朴素贝叶斯 matlab训练和测试输出

Markdown 文本换行_markdowntext 换行-程序员宅基地

文章浏览阅读1.6k次。Markdown 文本换行_markdowntext 换行

错误:0xC0000022 在运行 Microsoft Windows 非核心版本的计算机上,运行”slui.exe 0x2a 0xC0000022″以显示错误文本_错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行-程序员宅基地

文章浏览阅读6.7w次,点赞2次,收藏37次。win10 2016长期服务版激活错误解决方法:打开“注册表编辑器”;(Windows + R然后输入Regedit)修改SkipRearm的值为1:(在HKEY_LOCAL_MACHINE–》SOFTWARE–》Microsoft–》Windows NT–》CurrentVersion–》SoftwareProtectionPlatform里面,将SkipRearm的值修改为1)重..._错误: 0xc0000022 在运行 microsoft windows 非核心版本的计算机上,运行“slui.ex