search
HomeBackend DevelopmentPHP Tutorialwindows上制作PHP扩展

windows下制作PHP扩展

windows下制作PHP扩展
2011年01月26日
  转自:http://demon.tw/software/compile-php-on-windows.ht ml
  编译PHP扩展必需的一些头文件需要从php源码中获取,其中有一些配置性的头文件,需要做到下面第8步之后,也就是说php服务器程序可以使用别人已经编译好的安装包。 最近在学习编写PHP拓展,懒得装Linux,就研究了一下在Windows下编译PHP的方法,总算搭建好了在Windows下开发PHP拓展的环境。
  1、在C盘新建一个文件夹,C:\PHPDEV
  2、从官网下载最新版(我这里用的是5.2.14)的PHP源码,将下tar.gz或tar.bz2压缩包保存到C:\PHPDEV,解压到当前文件夹
  3、下载php win32 build extras并保存到C:\PHPDEV,解压到当前文件夹
  4、获取编译好的bison.exe和flex.exe,保存到桌面或者其他C:\PHPDEV以外的地方。分别打开压缩包并进入bin文件夹,将bison.exe和flex.exe复制到C:\Windows文件夹
  5、假定你已经安装了Visual Studio 2008(我个人不推荐用Visual Studio 2008,推荐使用Platform SDK Febrary 2003,官方的编译版本就是用这个PSDK编译的,但是这里用Visual Studio 2008做说明),开始菜单->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 Command Prompt,打开Visual Studio 2008命令提示行
  6、用cd命令将目录切换到C:\PHPDEV\php-5.2.14
  7、输入buildconf.bat,回车。这个批处理的作用是搜索所有的.w32文件并为你创建configure.js
  8、输入下面的命令
  cscript /nologo configure.js  without-xml  without-wddx  without-simplexml  without-dom  without-libxml  disable-zlib - without-sqlite  disable-odbc  disable-cgi  enable-cli  enable-debug  without-iconv  disable-ipv6
  为什么要disable和without那么多功能呢?因为这些功能需要的库文件并没有包含在PHP的源码包中(不然会很大),这些额外的库文件需要你自己去下载(要找全不是那么容易的)。如果你不禁用这些功能,会出现编译错误。
  9、输入nmake,回车
  10、第9步中可能会出现文件的编码错误,找到出现错误的文件,用EditPlus选择Western European (Windows)编码打开后另存为utf-8编码,重写nmake即可
  11、编译好以后切换到C:\PHPDEV\php-5.2.6\Debug_TS,测试一下
  php -r "echo 'hello,world';" 二、windows下开发PHP扩展
  转自:http://blog.csdn.net/linvo/archive/2009/04/17/4086 909.aspx
  第一步:准备
  1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
  2、解压源码包到d:\php_src
  3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为"linvo"
  4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
  5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
  第二步:编码
  6、编辑php_skeleton.h文件(头文件)
  在PHP_FUNCTION(confirm_linvo_compiled);下面编写
  PHP_FUNCTION(hello);
  声明一个hello函数
  7、编辑skeleton.c文件(主文件)
  在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
  PHP_FE(hello, NULL)
  这是函数入口,下面该写函数主体了
  找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
  PHP_FUNCTION(hello)
  {
  char *arg = NULL;
  int arg_len, len;
  char *strg;
  /* 接收参数 */
  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  return;
  }
  len = spprintf(&strg, 0, "Hello,%s", arg);
  RETURN_STRINGL(strg, len, 0);
  }
  第三步:编译
  8、运行cmd命令行,进入d:\php_src\ext\linvo目录
  9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
  10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
  第四步:使用
  11、将其拷入运行环境中的php扩展目录ext
  12、编辑php.ini添加extension=php_linvo.dll,重启apache
  13、在php文件中执行如下语句 
  echo hello('Linvo');
  将输出
  Hello,Linvo
  14、通过echo phpinfo();也可看到扩展已加载的信息
  linvo
  linvo support enabled 三、加载问题
  当生成的扩展放到文件夹中,并且修改了php.ini之后有可能出现生成的扩展dll并没有被php加载,可以从以下几个方面进行检查。
  1、创建一个php,并且使用phpinfo()函数打出当前php信息,找到php中真实加载的php.ini。
  2、是否已经重启过apache。
  3、查看apache的错误日志,看出现的有没有关于加载扩展出错的一些信息。 如果出现"PHP Warning:  PHP Startup: extname: Unable to initialize module\nModule compiled with module API=20090626, debug=0, thread-safety=1\nPHP    compiled with module API=20060613, debug=0, thread-safety=1\nThese options need to match\n in Unknown on line 0"这样的提示,说明编译时使用源码版本与php的应用程序不符合(并未要求完全一致的版本) ,如果出现不符合。
  4、写扩展时是否引用了其它dll,对于此种问题原因,详见:
  http://www.guyzyl.com/post-37.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor