win10 php7 zend,win10 编译php7.2 扩展

本文详细介绍了在Windows10系统中编译PHP7.2自定义扩展的步骤,包括安装Visual Studio 2019、下载PHP SDK工具、获取PHP7.2源码、编写扩展、解决编译错误到最后成功测试扩展的全过程。通过这个指南,读者可以了解如何在Windows环境下手动构建PHP扩展。
摘要由CSDN通过智能技术生成

win10 编译php7.2 自定义简单的扩展

1、环境准备

1.1、Visual C++ 编译器(安装vs2019)

官方支持使用微软的Visual C++ compilers。

Visual C++ 15.0 (Visual Studio 2017) for PHP 7.2, PHP 7.3 or PHP 7.4.

安装宇宙第一IDE——visual studio 2019 社区版(免费)。

d02fba8ac5c1a289a9ba359dfd781bdb.png

1.2、sdk-tool 下载

官方提供了工具,版本根据官方指定的,选择2.2

The PHP SDK 2.2+ is compatible with PHP 7.2 and above.

下载并切换到2.2 版本。

1

2git clone https://github.com/microsoft/php-sdk-binary-tools.git php-sdk

git checkout php-sdk-2.2.0

1.3、下载php7.2 源码1https://windows.php.net/downloads/releases/php-7.2.31-src.zip

1.4、php7.2

编译过程中用到了php 命令行,运行脚本。

1.5、git bash

2、编译前准备

2.1、利用sdk-tool 生成对应的开发目录

1、进入php-sdk 目录下

运行脚本phpsdk-vc15-x64.bat

90602bcbcfcc162dcad39c7164df4890.png

输入phpsdk_buildtree phpdev ,会生成并进入目录php-sdk\phpdev\vc15\x64

vc15 指的是编译环境15,x64 系统位数是64

2、安装依赖包

运行phpsdk_deps -u或phpsdk_deps --update --branch master。这是官方给的做法,但是网速不行。

上面的命令会把依赖安装到php-sdk\phpdev\vc15\x64\deps 目录下,只需要把以来下载并解压到该目录下就可以了。

notice:依赖地址

选择 vc15—–>x64 路径下的所有的文件下载并解压到deps 下。

28adba8b1d76c7ba61a63d15d1f3185c.png

2.2、编写扩展

2.2.1、生成扩展结构

把php7.2 的源码解压到php-sdk\phpdev\vc15\x64 目录下。使用bash 进入到/php-sdk/phpdev/vc15/x64/php-7.2.31/ext 目录下

输入:

1php.exe ext_skel_win32.php --extname=mytestfunction

c35428d80b79b469bb8333b4b65438e6.png

在当前目录下生成了文件夹mytestfunction

1aff74931850de60bb24d908bd131ea7.png

2.2.2、自定义函数

添加下面的代码到ext\mytestfunction\mytestfunction.c

1

2

3PHP_FUNCTION(mytestfunction) {

php_printf("This is my function PHP extension! \n");

}

并且修改

19ef2c27a03c4dcbabdf04f87bd6d442.png

3、编译扩展

1、切回到cmd 命令界面。启动脚本进入到编译环境,进入php7.2 源码目录,运行buildconf

f34e96a5c6dc4c9cf1523d93580d881d.png

输入configure --help 报错。

修改php-sdk\phpdev\vc15\x64\php-7.2.31\configure.js

78101e2a70260ea0ff039f0934f25eda.png

2、重新输入configure --help,命令参数出现在了help 的列表里

eb7dc4aa51002f18a8501dd45fcde689.png

3、运行configure --disable-all --enable-cli --enable-mytestfunction=shared 命令,添加编译时的配置(添加扩展的配置参数)。

结果里出现了自定义的可用扩展

431982912442a20b3ea9e71b41ffeefd.png

4、运行nmake 构建php

报错:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18D:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31

$ nmake

Microsoft (R) 程序维护实用工具 14.26.28806.0 版

版权所有 (C) Microsoft Corporation。 保留所有权利。

Recreating build dirs

type ext\pcre\php_pcre.def > D:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\php7ts.dll.def

"D:\Windows Kits\10\bin\10.0.18362.0\x64\mc.exe" -h win32\ -r D:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ -x D:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ win32\build\wsyslog.mc

MC: Compiling win32\build\wsyslog.mc

"cl.exe" /D COMPILE_DL_MYTESTFUNCTION /D MYTESTFUNCTION_EXPORTS=1 /nologo /I . /I main /I Zend /I TSRM /I ext /D _WINDOWS /D WINDOWS=1 /D ZEND_WIN32=1 /D PHP_WIN32=1 /D WIN32 /D _MBCS /W3 /D _USE_MATH_DEFINES /FD /wd4996 /Zc:inline /Zc:__cplusplus /MP /LD /MD /W3 /Ox /D NDebug /D NDEBUG /D ZEND_WIN32_FORCE_INLINE /GF /D ZEND_DEBUG=0 /D ZTS=1 /I "D:\git-repository\php-sdk\phpdev\vc15\x64\deps\include" /D FD_SETSIZE=256 /FoD:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ext\mytestfunction\ /FpD:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ext\mytestfunction\ /FRD:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ext\mytestfunction\ /FdD:\git-repository\php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\ext\mytestfunction\ /c ext\mytestfunction\mytestfunction.c

mytestfunction.c

ext\mytestfunction\mytestfunction.c(57): warning C4101: “len”: 未引用的局部变量

ext\mytestfunction\mytestfunction.c(150): error C2065: “zif_mytestfunction”: 未声明的标识符

ext\mytestfunction\mytestfunction.c(150): error C2099: 初始值设定项不是常量

ext\mytestfunction\mytestfunction.c(150): warning C4047: “初始化”:“const _zend_internal_arg_info *”与“uint32_t”的 间接级别不同

NMAKE : fatal error U1077: “D:\MicrosoftVisualStudio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\cl.exe”: 返回代码“0x2”

Stop.

66fe20a61544cbdd5388b5301d684049.png

5、解决

修改ext\mytestfunction\mytestfunction.c

1

2

3

4PHP_FUNCTION(mytestfunction)

{

php_printf("This is my function PHP extension! \n");

}

往上放一放,像图里这样

33c64b52e39404528564079d791a43ab.png

notice:重复 3 的操作(重新buildconf 等等)

configure 的错通过编辑php-sdk\phpdev\vc15\x64\php-7.2.31\ext\mytestfunction\config.w32 解决

69323743423f4cc47a53b3b272a10826.png

6、解决完报错后,等好一会,php 构建成功

7d5e580857e040135852e037ec81537c.png

就在源码目录下——php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS

当前目录下执行.\php.exe -v 还会显示当前目录下的php 的版本信息

29ee43a8976612782aeb1f21bdd18ead.png

当前目录下没有php.ini 配置文件,从源码目录下复制php.ini-development 到当前目录并改名。

然后修改php.ini php-sdk\phpdev\vc15\x64\php-7.2.31\x64\Release_TS\php.ini

添加

a2f170d87e81d9dc3713f98c504c08e7.png

添加

d0ee848e08b1672fe00e19893f74a1f0.png

4、测试扩展

源码目录下新建test.php

1

2<?php

mytestfunction();

运行命令

1.\php.exe test.php

4ff7e99b9e8de59a380d175ca904ef50.png

5、修改扩展

重新configure 和nmake

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值