将PHP扩展编译为非线程安全 [英] Compiling a PHP extension as non-thread-safe

查看:636
本文介绍了将PHP扩展编译为非线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译imagemagick(imagick)扩展以在Windows上的非线程安全的环境中使用。



我使用PHP 5.3.10和已经设置Visual C ++ express作为我的编译环境。
问题是我使用非线程安全版本的PHP作为Apache 2.2中的FCGI模块。



因此,我的PHP提供a php5.lib 而不是 php5ts.lib 。我相信这是我得到这些错误的原因:

  imagick.obj:error LNK2019:未解析的外部符号__imp__tsrm_mutex_alloc函数_zm_startup_imagick 

我只是在linux上编译和编译的东西,所以不太确定如何做



如何编译扩展以使它是非线程安全的?



< hr>

我下载了PHP 5.3.10的threadsafe二进制文件以获取php5ts.lib的副本。我现在可以编译扩展。



我猜测将 ZTS 预处理器指令设置为 1 0 会导致扩展被编译为线程安全或非线程安全。 (不太确定,所以如果有人可以让我知道,如果这是正确的,这将是非常感谢:))



然后我设置了一个虚拟机运行Windows 7并安装了最新版本的WAMP。原因是它使用了线程安全版本的PHP。



我把DLL放在PHP安装的ext文件夹中,并在 php中启用它。 ini 。但是,即使在WAMP中尝试ts和nts版本之后,我也会得到:

  PHP警告:PHP启动:无法加载动态库'c:/wamp/bin/php/php5.3.10/ext/imagick.dll' - 找不到指定的模块。 
in Unknown 0 on $ 0

但是事情是 c :/wamp/bin/php/php5.3.10/ext/imagick.dll'确实存在,是的,我已经检查了很多次。



然后我将ImageMagick降级到6.6.2-10-Q16,但仍然看到同样的问题。



同样的问题发生在我的dev机器上运行nts Apache 2.2上的PHP 5.3.10版本(全部手动安装)。






看起来我可能错了, code> ZTS 预处理器。如果我设置 ZTS = 0 并编译,使用依赖关系walker在编译的dll仍然显示它需要 php5ts.dll 它只存在于线程安全版本的PHP上。






我对依赖者Walker做了更多的修改,链接到msvc100d.dll。然后我在预处理器定义中删除了 ZTS ,并且能够使用 php5.lib 而不是 php5ts.dll



但是,当加载扩展时,我仍然得到:

  PHP警告:PHP启动:无效的库(可能不是PHP库)'imagick.dll'在未知的行0 

和依赖关系walker的错误:

 错误:由于隐式依赖模块中缺少导出函数,至少一个模块具有未解析的导入。 
错误:找到具有不同CPU类型的模块。
警告:找不到至少一个延迟负载相关性模块。
警告:由于延迟加载相关模块中缺少导出功能,至少有一个模块具有未解析的导入。






安装visual c ++ express 2008并使用平台工具包v






找到解决方案!请参阅我的回答。


解决方案

我希望这可以帮助那些编译php扩展在Windows上的未来。我所做的是基于: https://wiki.php.net/internals/windows/stepbystepbuild



注意:我在这个网站上看到一个答案,表示不支持在Windows上编译PHP扩展程序。基于我昨天的实验,它似乎还有很多工作,所以我使用PHP构建过程。



最好的方法是使用PHP构建


  1. 安装Visual C ++ Express 2008(任何版本都可以,但表达是免费的)我们希望6.1版本不是任何旧版本或更新版本。)


  2. 安装 SVN客户端工具,如果您尚未这样做,并重新启动 PATH 变量生效。

    li>
  3. 获取 PHP SDK二进制工具并将其解压缩到 C:\ php-sdk


  4. > CMD Shell 在 Microsoft Windows SDK v6.1 下运行 cd C:\php-sdk


  5. 执行 setenv / x86 / xp / release 32位版本。您可以使用 / x64 构建64位版本(我没有尝试过)。


  6. 运行 bin\phpsdk_setvars.bat


  7. 运行 bin\phpsdk_buildtree .bat php53dev


  8. 下载 PHP源代码,并将其解压缩到 C:\php-sdk\php53dev\vc9\x86 ,以便最终以 C:\php-sdk\php53dev\vc9\x86\php5.3-xyz


  9. 下载最新的 deps 并提取所有内容至 C :\php-sdk\php53dev\vc9\x86


  10. 运行 cd C:\php-sdk\php53dev \vc9\x86


  11. 从svn存放区检出您的扩充功能的最新版本: svn co http://svn.php.net/repository/pecl/imagick/trunk pecl / imagick 我在这里查看想象。


  12. 安装任何依赖项。在我的情况下,我需要安装ImageMagick,所以我下载了最新的推荐 Windows二进制。您需要在安装后重新启动以更新您的 PATH 变量,但您可以在编译后这样做。


  13. 复制包含目录从ImageMagick安装目录下载到 C:\ php-sdk\php53dev\vc9\x86\deps\include

    li>
  14. 将ImageMagick安装目录中的 lib 目录复制到 C:\ php- sdk\php53dev\vc9\x86\deps\lib


  15. 现在,让我们构建:run cd C:\ php-sdk\php53dev\vc9\x86\php5.3-xyz 然后 buildconf configure --help 。看看帮助正确。它会显示所有配置标志和任何可用的扩展启用。在我的情况下,由于我添加了imagick,我有一个选项 - with-imagick


  16. 运行config: configure --enable-cli --with-imagick = shared --disable-zts 如果你想构建非线程安全的二进制使用 - disable-zts ,否则删除该配置标志。我不知道为什么,但有时扩展使用 - with-myextension 标志,有时 - enable-myextension ,因此请检查 configure --help 以使用该标志。如果在扩展标志之后添加 = shared ,它将被编译为单独的DLL,这是我们想要的。


  17. 编译:运行 nmake ,然后 nmake snap


  18. 完成:)检查 C:\php-sdk\php53dev\vc9\x86\php-5.3.10\Release (非线程安全)或 C:\php-sdk\php53dev\vc9\x86\php-5.3.10\Release_TS -安全)。你编译的二进制文件应该在那里。编译的扩展(pecl)应该在类似于 pecl-5.3.10 -nts-Win32-VC9-x86.zip 的ZIP文件中。打开此文件并将expandio DLL解压缩到PHP安装中的 ext 文件夹。


  19. PHP扩展并重新启动Web服务器。


  20. 如果扩展程序无法正常工作,请运行 phpinfo() ,请确保您重新启动以更新 PATH 变量。在我的例子中,我需要在我的 PATH 变量中有imagemagick安装目录。



  21. 利润!



I am trying to compile the imagemagick (imagick) extension to use in a non-thread-safe environment on windows.

I am using PHP 5.3.10 and have set up Visual C++ express as my compiling environment. The problem is that I am using a non-thread-safe version of PHP as a FCGI module in Apache 2.2.

Thus, my PHP is supplied with a php5.lib and not a php5ts.lib. I believe this is the reason why I am getting these errors:

imagick.obj : error LNK2019: unresolved external symbol __imp__tsrm_mutex_alloc referenced in function _zm_startup_imagick

I have only ever built and compiled things on linux, so am not too sure how to do this on a windows environment.

How can I compile the extension so that it is non-thread-safe?


I downloaded the threadsafe binary of PHP 5.3.10 to get a copy of php5ts.lib. I was then able to compile the extension.

I am guessing that setting the ZTS preprocessor directive to either 1 or 0 would cause the extension to compile as either threadsafe or non-thread-safe. (Not too sure about this, so if someone could let me know if this is correct or not, it would be much appreciated. :) )

I then set up a virtual machine running Windows 7 and installed the latest version of WAMP. Reason being that it used a threadsafe version of PHP.

I placed the dll in the ext folder in the PHP installation and enabled it in php.ini. However, even after trying both the ts and nts version in WAMP, I would get:

PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.3.10/ext/imagick.dll' - The specified module could not be found.
 in Unknown on line 0

But the thing is that c:/wamp/bin/php/php5.3.10/ext/imagick.dll' does exist, and yes I have checked this a lot of times.

I then downgraded ImageMagick to 6.6.2-10-Q16, but still see the same problem.

The same issue occurs on my dev machine running a nts version of PHP 5.3.10 on Apache 2.2 (all installed manually).


Looks like I could be wrong regarding the ZTS preprocessor. If I set ZTS=0 and compile, using dependency walker on the compiled dll still shows that it requires php5ts.dll which is only present on threadsafe versions of PHP.


I did more tinkering with dependency walker and found that I had to statistically link to msvc100d.dll. I then removed ZTS in the preprocessor definition and was able to compile using php5.lib instead of php5ts.dll. I think this should get me a non-ts dll.

However, when loading the extension, I am still getting:

PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) 'imagick.dll' in Unknown on line 0

And errors from dependency walker:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.


Installed visual c++ express 2008 and compiled using platform toolkit v90, but still facing the same issues.

Any tips appreciated :)


Solution Found! See my answer.

解决方案

Got it! I hope this can help those compiling php extensions on windows in the future. What I have done is based on: https://wiki.php.net/internals/windows/stepbystepbuild

Note: I read an answer on this site that said compiling PHP extensions by itself on windows is unsupported. Based on my experiments yesterday, it seems to be a lot of work as well, so I am using the PHP build process.

The best way is to use the PHP build process.

  1. Install Visual C++ Express 2008 (any version will do, but express is free) and Windows SDK 6.1 (note that we want the 6.1 version not anything older or newer).

  2. Install the SVN client tools if you haven't done so and restart for your PATH variable to take effect.

  3. Get the PHP SDK binary tools and extract it to C:\php-sdk.

  4. Start the CMD Shell under Microsoft Windows SDK v6.1 in your start menu and run cd C:\php-sdk.

  5. Run setenv /x86 /xp /release this creates a 32-bit release. You can use /x64 to build a 64-bit release (I have not tried this).

  6. run bin\phpsdk_setvars.bat

  7. run bin\phpsdk_buildtree.bat php53dev

  8. Download the PHP source code and extract it to C:\php-sdk\php53dev\vc9\x86 so that you end up with C:\php-sdk\php53dev\vc9\x86\php5.3-xyz

  9. Download the latest deps and extract everything to C:\php-sdk\php53dev\vc9\x86. You may be prompted to overwrite any existing folders, so press yes.

  10. run cd C:\php-sdk\php53dev\vc9\x86

  11. Check out the latest version of your extension from the svn repository: svn co http://svn.php.net/repository/pecl/imagick/trunk pecl/imagick I am checking out imagick here.

  12. Install any dependencies. In my case, I need to install ImageMagick, so I downloaded the latest recommended windows binary. You will need to restart after installation to update your PATH variable, but you can do this after you have compiled. Make sure you install the C/C++ headers and includes as well.

  13. Copy the contents of the includes directory from the ImageMagick install directory to C:\php-sdk\php53dev\vc9\x86\deps\include.

  14. Copy the contents of the lib directory from the ImageMagick install directory to C:\php-sdk\php53dev\vc9\x86\deps\lib

  15. Now, let's build: run cd C:\php-sdk\php53dev\vc9\x86\php5.3-xyz then buildconf then configure --help. Look at the help properly. It will show you all config flags and any avaliable extensions to enabled. In my case, since I added imagick, I had an option called --with-imagick

  16. Run config: configure --enable-cli --with-imagick=shared --disable-zts If you want to build a non-thread-safe binary use --disable-zts, otherwise remove that configuration flag. I am not sure why, but sometimes extensions use the --with-myextension flag and sometimes --enable-myextension, so check configure --help for the flag to use. If you add =shared after the extension flag, it will be compiled as a separate DLL, which si what we want.

  17. Compile: run nmake then nmake snap.

  18. Done :) Check C:\php-sdk\php53dev\vc9\x86\php-5.3.10\Release (non-thread-safe) or C:\php-sdk\php53dev\vc9\x86\php-5.3.10\Release_TS (thread-safe). Your compiled binaries should be in there. The compiled extensions (pecl), should be in a ZIP file similiar to pecl-5.3.10-nts-Win32-VC9-x86.zip. Open this file and extract the extensio DLL to your ext folder in your PHP installation.

  19. Enable the PHP extension and restart your webserver. Run phpinfo() and check that the extension is enabled.

  20. If the extension does not work properly, make sure you reboot to update your PATH variable. In my case, I need to have the imagemagick install directory in my PATH variable.

  21. ???

  22. Profit!

这篇关于将PHP扩展编译为非线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆