How to compile PHP on Windows with Visual Studio 2022 Community Edition

Overall problem

Microsoft only offers their latest installer called VisualStudioSetup.exe for community edition which is at the time of writing VS2022: https://visualstudio.microsoft.com/vs/older-downloads/

And I can’t compile a compatible PHP-binary with it.

Technical details

The problem I’m facing is that when you compile PHP from source (PHP 8.1, PHP 8.2, PHP 8.3) it is linked to MSVC 14.40. When you download from https://windows.php.net it is linked to MSVC 14.29.

Therefore the self-compiled PECL extensions are not compatible with the downloaded PHP binary.

Symptom

They won’t get loaded on startup with the following message:
Warning: PHP Startup: Can't load module 'php_jsmin.dll' as it's linked with 14.40, but the core is linked with 14.29 in Unknown on line 0

This does apply to all self-compiled extensions, not only jsmin.

Approaching the problem

I can’t install MSVC 14.29 only, because PHP needs two components:

  • MSVC v142 – VS 2019 C++ x64 build tools (v14.29)
  • C++ CMake tools for Windows

C++ CMake tools for Windows has a damn dependency:

  • MSVC v143 – VS 2022 C++ x64 build tools (v14.40)

The php compile environment now always links against v14.40. This was no problem with Visual Studio 2019 Setup, but Microsoft forbids to download old community versions – You can only download Visual Studio 2022 Setup with that before mentioned dependency problem.

Question

How can I link against MSVC 14.29 when using Visual Studio 2022 Setup to compile PHP from source.

Thank you very much in advance!