How to compile a PHP extension (DLL file) in Windows with Visual Studio (2024)

Learn how to compile PHP extensions from its source code using Visual Studio in a Windows environment.

After working on a very old project based on PHP 5.3, where the APC library was required to make it work, i knew how difficult is to find the APC extension to make it work on the Windows platform. For Linux, the process can be pretty easy with PECL, however in Window this was a real problem.

That's why today i'm going to show you how to compile a PHP extension from its source code from scratch in a Windows environment, specifically with the APC library and PHP 5.3.8.

Requirements

  • Visual Studio: the version needs to provide some compatibility with the accepted versions of Visual C++, see the first point for more information.
  • PHP Source Code: The source code of PHP for Windows at the version that you need to be compatible with the extension.
  • Extension Source Code: The source code of the extension for PHP that you want to compile into a dynamic link library (DLL file).

Having said that, let's get started with the compilation !

1. Prepare Visual Studio environment

Before getting started with the compilation of a PHP extension, you need to know that not every VS compiler is compatible with any version of PHP, so you will need to know which Visual Studio you need to use according to the version of PHP that you want compile your extension (obtain DLL file). Check out the following table that specifies which version of Visual Studio you need to compile your desired PHP version:

  • Yes: this version is supported and the PHP team provide binaries built with this compiler
  • No: Not supported
PHP Version5.2.x5.3.x5.4.x5.5.x5.6.x7.0.x7.1.x7.2.x64bits (***)
Visual C++ 6 (SP6 only)YesYesNoNoNoNoNoNoNo
Visual C++ 7/7.1 (2002, 2003, 2003+sp1)Yes (**)Yes (**)NoNoNoNoNoNoNo
Visual C++ 8 (VS 2005)Yes (**)NoNoNoNoNoNoNoNo
Visual C++ 9 (VS 2008 SP1 only)Yes (**)YesYesYes (**)NoNoNoNoNo
Visual C++ 11 (VS 2012)NoNoYes (**)YesYesYes (**)NoNoYes
Visual C++ 14 (VS 2015)NoNoNoNoNoYesYes(**)Yes(**)Yes
Visual C++ 15 (VS 2017)NoNoNoNoNoYes(**)Yes(**)YesYes
  • (*) May be dropped before the final release
  • (**) Not officially supported but known to work
  • (***) Supported for this given compiler according to the current official support (or known to work)

So to make it easy to understand, if you are willing to compile an extension for PHP 7, you will need the source code of PHP 7, the code of the extension as well and at least Visual Studio 2012 assuming that you want to compile it for PHP 7.0, because if you want it for PHP 7.1, then you would need at least Visual Studio 2015.

Knowing this, if you are sure that you have the correct version you shouldn't face any trouble while you compile the extension. Otherwise you will face weird exceptions during the configuration of the build:

Saving configure options to config.nice.batChecking for cl.exe ... <in default path>Detected compiler undefinedC:\php-src\configure.js(1817, 9) Microsoft JScript runtime error: 'length' is null or not an object

Note

In this tutorial we'll build the APC extension for PHP 5.3.8, so in our case we'll need to work with Visual Studio 2008 SP1 for visual c++ 9.

2. Open Visual Studio Command Prompt

In order to run some administrative commands related to the development and build of your extension, you will need a command prompt with the context of Visual Studio. Namely theDeveloper Command Prompt for VS that you can find at the path:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\<Your Visual Studio Version e.g 2008 or 2017>\Visual Studio Tools

Here you will find an executable of the command prompt that has access to all the tools that you will need to build the extension, execute it with administrator rights:

How to compile a PHP extension (DLL file) in Windows with Visual Studio (1)

As mentioned, is a normal command prompt that contains the tools that we need for the next steps:

How to compile a PHP extension (DLL file) in Windows with Visual Studio (2)

Let it open as we'll need in the step 5.

3. Download PHP Source Code

Now that you have the necessary tools to compile PHP along with your extension, you will need as well the source code of PHP. You can get the source code of the PHP version that you need from the official releases webpage of PHP here.

Once you download the tar/zip file with the code, extract it into a directory with a short path to prevent any problem with the length of the paths in Windows. In our case, we'll extract the source code of PHP 5.3.8 in the c:/php-src directory:

How to compile a PHP extension (DLL file) in Windows with Visual Studio (3)

We'll run the commands to build PHP along with your code in this directory within the command prompt later. It's worth to mention that the ext folder will contain the code of your extension but we will add it in the next step.

4. Prepare Extension Source Code

In order to build your extension, either you build an open source extension or create your own extension. In our case, we will build the APC extension, the version 3.1.6 specifically for PHP 5.3.8 (the same version of our PHP source code). This will ensure that the generated dll of PHP will be compatible with the PHP version that we need.

Once you download the source code of your extension (or you write your own code for it), extract it inside the ext folder of the PHP source code. In our case, the directory for the extension would be c:/php-src/ext/apc and it will contain the code of the extension (this is very important as the extension will be compiled along as a dynamic library with PHP):

How to compile a PHP extension (DLL file) in Windows with Visual Studio (4)

Be sure as well that theconfig.w32 file in the root directory of the source code of the extension, in the line that registers the extension with the EXTENSION function, the third parameter is set to true in order to generate a DLL (dynamic mode):

EXTENSION('apc', apc_sources, true);

Because as you may know you can integrate the extension directly with PHP, but in this tutorial we are showing how to generate the DLL file that in our case will be php_apc.dll.

5. Configure PHP and Extension build

Go back to the visual studio developer command prompt and switch to the directory of the source code of PHP:

cd c:/php-src

Then run the buildconf.bat file with the --force argument:

buildconf --force

This will generate a new .bat file namely configure.bat that will have the configuration of your PHP build. Run the bat with the following command and enable your extension using the --enable-<extension_folder_name> argument. In our case, the extension is APC and we stored the source code in the c:/php-src/ext/apc directory, so the extension name will be APC. Run the command to configure it:

configure --disable-all --enable-cli --enable-apc

Important note

If you run the previous command and you face an exception because bison.exe haven't been found:

  • Saving configure options to config.nice.bat
  • Checking for cl.exe ... <in default path>
  • Detected compiler MSVC9 (Visual C++ 2008)
  • Detected 32-bit compiler
  • Checking for link.exe ... C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN Checking for nmake.exe ... <in default path>
  • Checking for lib.exe ... <in default path>
  • Checking for bison.exe ... <not found>
  • ERROR: bison is required

Download the binaries of Bison from the website at Source Forge here. Then paste the bison.exe and m4.exe at the bin directory of Visual Studio e.gC:\Program Files (x86)\<Your Microsoft Visual Studio Version e.g 9.0>\VC\bin. This will make the bison executable accesible to the path and the exception bison is required won't appear anymore.

The --disable-all flag will help us to prevent any other exception of extensions that we don't need, just focusing on the APC extension that we need. And that's it, you will get a verbose output that includes a list with all the extensions that will be built along with PHP and its mode e.g Static (built-in classes in PHP) or Shared (a Dynamic Link Library dll):

How to compile a PHP extension (DLL file) in Windows with Visual Studio (5)

As expected, our extension of PHP will be built into a DLL file just as we want it. By default, this will create a "Thread Safe" build (PHP 5.3.8 TS). If you want a non thread safe build, just add the--disable-ztsflag to the configure command.

6. Build PHP and Extension

Finally, run the nmake command to start the build of your extension with PHP within the directory of the PHP source code with the developer command prompt.NMake is a make utility provided by Microsoft and available in Visual Studio. It is a handy tool for creating automated builds.

Just run the command:

nmake

This will start the compilation and will take a while. It will generate a verbose output as well with warnings etc:

How to compile a PHP extension (DLL file) in Windows with Visual Studio (6)

Once it finishes, a new directory will appear inside the c:/php-src directory, namelyRelease orRelease_TS according to the Thread Safety configuration. There you will find a very basic built of PHP, but most important, the reason why you exclusively built PHP, your extension DLL file (in our case php_apc.dll):

How to compile a PHP extension (DLL file) in Windows with Visual Studio (7)

Thanks

This article is a recopilation of multiple sources that you may find useful as well during the compilation of an open source extension or your own extension:

Happy coding !

How to compile a PHP extension (DLL file) in Windows with Visual Studio (2024)

FAQs

How to compile DLL file in Visual Studio? ›

This walkthrough covers these tasks:
  1. Create a DLL project in Visual Studio.
  2. Add exported functions and variables to the DLL.
  3. Create a console app project in Visual Studio.
  4. Use the functions and variables imported from the DLL in the console app.
  5. Run the completed app.
Dec 9, 2021

How to compile PHP on Windows? ›

Compiling and building ¶
  1. Open a command prompt which is used to build PHP.
  2. Go to the root folder where PHP sources are present.
  3. Run the command: cscript.exe win32\build\buildconf.js.
  4. Run the command: configure.bat --help. ...
  5. Run the command: configure.js [all options used to build PHP] --enable-wincache. ...
  6. Run the command: nmake.

How to create a DLL file in PHP? ›

Having said that, let's get started with the compilation !
  1. Prepare Visual Studio environment. ...
  2. Open Visual Studio Command Prompt. ...
  3. Download PHP Source Code. ...
  4. Prepare Extension Source Code. ...
  5. Configure PHP and Extension build. ...
  6. Build PHP and Extension.
Sep 1, 2018

How to create PHP File in Visual Studio? ›

Create your PHP project
  1. Select Create a new project from the start page or File > New > Project… from the title bar.
  2. In the language combo box select PHP, select PHP Web Project, then select Next.
  3. Name your project and select Create.
  4. At this point, you can select the project template.
Nov 3, 2022

Can Visual Studio run dll files? ›

The Microsoft Windows Visual Studio is a program that allows you to view, edit and build code into a DLL file.

Can Visual Studio open dll files? ›

A DLL (dynamic-link library) is a library that contains code and data that can be used by more than one app. You can use Visual Studio to create, build, configure, and debug DLLs.

Is there a way to compile PHP? ›

phc allows you to compile PHP programs into shared libraries, which can be uploaded to the server. The PHP program is compiled into binaries. It's done in such a way as to support eval s, include s, and the entire PHP standard library.

How to compile a PHP file? ›

Preparing environment: packages necessary to compile PHP from source
  1. gcc. GCC (GNU C Compiler) is an open source C compiler, widely used by most C projects out there. ...
  2. libc-dev. ...
  3. autoconf. ...
  4. bison. ...
  5. re2c. ...
  6. make. ...
  7. Install required packages. ...
  8. Build the configuration script.
May 24, 2021

How do I run and compile a PHP program? ›

To run PHP for the web, you need to install a Web Server like Apache and you also need a database server like MySQL. There are various web servers for running PHP programs like WAMP & XAMPP. WAMP server is supported in windows and XAMP is supported in both Windows and Linux.

Can we use DLL in PHP? ›

If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.

How to create PHP file in Windows 10? ›

Installing PHP
  1. Step 1: Download the PHP files. Get the latest PHP x64 Thread Safe ZIP package from https://windows.php.net/download/.
  2. Step 2: Extract the files. ...
  3. Step 3: Configure php.ini. ...
  4. Step 4: Add C:\php to the PATH environment variable. ...
  5. Step 5: Configure PHP as an Apache module. ...
  6. Step 6: Test a PHP file.
Dec 27, 2022

How to create a DLL file in command prompt? ›

Step to create DLL at the Dos Command Prompt
  1. Run Dos Command Prompt.
  2. Create a working directory that contains the necessary files see above.
  3. Run f90 /dll myfile.for solver.lib (if the source file name is myfile.for)

Can you develop PHP in Visual Studio? ›

PHP in Visual Studio Code. Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.

How to format PHP code in Visual Studio? ›

The formatting can be triggered explicitly by two actions:
  1. Format Document ( Shift+Alt+F ) - Format the entire active file.
  2. Format Selection ( Ctrl+K Ctrl+F ) - Format the selected text.

How to set PHP path in Visual Studio Code? ›

Open the VS Code settings by going to Preferences > Settings (on macOS) or File > Preferences > Settings (on Windows). In the search box, type "php. validate. executablePath" and select the "Edit in settings.

How to create DLL file in Visual Studio Code? ›

Write a Sample DLL

In Microsoft Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type. The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.

How to add DLL file in Visual Studio Code? ›

Use Project -> Add Project Reference. Then in dialog window navigate to Browse tab and use Browse to find and attach your dll.

How to embed DLL in Visual Studio? ›

Just right-click your project in Visual Studio, choose Project Properties -> Resources -> Add Resource -> Add Existing File… And include the code below to your App.

How to decompile a DLL in Visual Studio? ›

To do this, go to the Modules window and from the context menu of a . NET assembly, and then select the Decompile Source to Symbol File command. Visual Studio generates a symbol file for the assembly and then embeds the source into the symbol file. In a later step, you can extract the embedded source code.

How to test a DLL in Visual Studio? ›

If the DLL project exports the functions that you want to test, then you can add a reference to the code project from the test project.
  1. Create a Native Unit Test Project. ...
  2. In Solution Explorer, right-click on the test project, then choose Add > Reference.
  3. Select Projects, and then the project to be tested.
Mar 9, 2023

How do I view the contents of a DLL in Visual Studio? ›

  1. Applies to: Visual Studio Visual Studio for Mac Visual Studio Code.
  2. During Visual Studio debugging, the Modules window lists and shows information about the DLLs and executables (.exe files) your app uses.
  3. To open the Modules window, while you're debugging, select Debug > Windows > Modules (or press Ctrl + Alt + U).
Apr 29, 2022

Why PHP is not compiled? ›

PHP is an interpreted language. It can be compiled to bytecode by third party-tools, though. "bytecode" is a term used for VM specific pseudo instructions, which are not native hence can't be regarded as "compilation" in this context. "object code" is just bytecode for the CPU's instruction decoder.

Is PHP applications Cannot be compiled? ›

A php applications cannot be compiled b php cannot be

To define a constant you have to use define () function.

How to compile PHP in command prompt? ›

all you need is just to write php index. php if index. php is your file name. then you will see that, the file compiled and showing it's desired output.

Which server is used to compile PHP code? ›

Apache web server

The -dev package is required in order to build PHP.

How to run PHP file step by step? ›

Executing PHP files ¶
  1. Tell PHP to execute a certain file. $ php my_script.php $ php -f my_script.php. ...
  2. Pass the PHP code to execute directly on the command line. $ php -r 'print_r(get_defined_constants());' ...
  3. Provide the PHP code to execute via standard input ( stdin ).

How does PHP compilation work? ›

Compilation. Without the use of JIT, PHP in its standard form is compiled from the generated AST to OPCode, not – as is the case with JIT – into machine code. The compilation process is carried out by recursively traversing the AST, as part of which some optimisations are made as well.

How to run PHP project from command line in Windows? ›

On Windows
  1. Download it from the official site.
  2. Extract the file and store it in a folder, preferably named php , in your C just like so: c:/php.
  3. Next, place this folder in your environment variable path so that it can be recognized by the command line.

What is the extension of PHP file? ›

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.

Can you run PHP from command line? ›

Although the PHP scripting language has been mainly developed for the creation of dynamic websites, you can also execute PHP scripts completely independent of the web server. For this purpose, you can call the PHP script on the command line with the so-called PHP Command Line Interpreter (short: PHP CLI).

Where to put PHP DLL? ›

Copy the php_wincache. dll file into the PHP extensions folder. Typically this folder is called "ext" and it is located in the same folder with all PHP binary files. For example: C:\Program Files\PHP\ext .

What program runs PHP files? ›

Amongst them, PHP XAMPP and WampServer are the most popular. While WampServer is only available for the Windows platform, XAMPP is a cross-platform application that can run on Windows, Linux, and macOS. Hence, in this tutorial, you will learn PHP using XAMPP.

Can a DLL be an executable? ›

It is not possible to directly execute a DLL, since it requires an EXE for the operating system to load it through an entry point, hence the existence of utilities like RUNDLL.

How to install PHP compiler in Windows 10? ›

Install PHP on Windows manually

You have to download zipped binary version of PHP from http://www.php.net/downloads.php. Extract the downloaded file to php folder which must be created at the root of any of your windows drives (e.g. c:\php or d:\php). Enter php folder and rename the file php. ini-recommonded to php.

What is the easiest way to install PHP on Windows? ›

The fastest and easiest way to install PHP on Internet Information Services (IIS) is by using the Microsoft® Web Platform Installer (Web PI). Web PI completely automates setting up IIS, FastCGI, and the latest version of PHP from the php.net Web site.

Where to create PHP script? ›

A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.

How is DLL file generated? ›

In Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type. The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.

How to register DLL in Visual Studio command prompt? ›

Click Start > All Programs > Accessories and right-click on "Command Prompt" and select "Run as Administrator" OR in the Search box, type CMD and when cmd.exe appears in your results, right-click on cmd.exe and select "Run as administrator" At the command prompt, enter: REGSVR32 "PATH TO THE DLL FILE"

How to register DLL in Windows 10 command prompt? ›

How to Register DLL Files on Windows?
  1. Press Win+R to open Run.
  2. Type the reg DLL command: regsvr32 “[the path of the DLL file]”. The following is an example:
  3. Click OK to execute the reg DLL command.
  4. You will receive a confirmation message once the DLL file has been successfully registered.
Nov 24, 2020

How to install PHP extension in Visual Studio? ›

Basic Installation

If you already have Microsoft Visual Studio installed, you can jump directly to the Manage Extensions dialog and search for PHP Tools for Visual Studio. Once downloaded, you'll be prompted to close Visual Studio. The package will be installed and Visual Studio configured for the PHP development.

How to install Visual Studio Code in Windows 10 for PHP? ›

How To Install VSCode For PHP On Windows
  1. Step 1 - Download Visual Studio Code. We can download Visual Studio from the official download page. ...
  2. Step 2 - Install Visual Studio Code. ...
  3. Step 3 - Marketplace. ...
  4. Step 4 - Install Extensions. ...
  5. Step 5 - Getting started with PHP - Hello World.
Aug 16, 2019

How to write code to file PHP? ›

PHP Write to File - fwrite()

The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

How to open PHP file in browser using Visual Studio Code? ›

Open PHP/HTML/JS In Browser
  1. Click the button Open In Browser on StatusBar.
  2. In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser.
  3. Use keybindings Shift + F6 to open more faster (can be changed in menu File -> Preferences -> Keyboard Shortcuts )
Dec 18, 2018

How to format code in Visual Studio command? ›

In an existing project, open the document that you want to format, right-click inside the document, and select Format Document. In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F.

How to get PHP path to file? ›

In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.

How to check PHP version in Visual Studio Code? ›

1. Type the following command, replacing [location] with the path to your PHP installation. 2. Typing php -v now shows the PHP version installed on your Windows system.

How to add PHP extension to path? ›

Installing the PHP extension on Windows
  1. Locate the php. ini file for your PHP installation, and open it in a text editor. ...
  2. Copy the file php-5. x.y_sqlanywhere. ...
  3. Add the following line to the Dynamic Extensions section of the php. ...
  4. Make sure that the Bin32 subdirectory of your SQL Anywhere installation is in your path.

How to compile DLL from source code? ›

  1. you need to install Visual Studio.
  2. need to install SKSE source code and add it as a reference properly to Visual Studio.
  3. add the dll source code as a project to Visual Studio (either by using git and/or releases if available)
  4. compile, test, and debug.
  5. copy dll to the correct Skyrim game folder.
  6. test in game.
Aug 20, 2022

How to run a DLL in Visual Studio C#? ›

Using DLL File
  1. Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows Forms application".
  2. Design the form as in the following image:
  3. Add a reference for the dll file, "calculation. ...
  4. Select the DLL file and add it to the project.

How to compile exe with dll? ›

Answers
  1. Right click on the project and choose properties.
  2. Got to the Publish Tab.
  3. Click on Application files.
  4. Make sure the DLL is in the Download Group ,"Required"
Aug 29, 2006

How to convert DLL to EXE Visual Studio? ›

You cannot just simply convert any . dll file to program executable .exe.
...
2 Answers
  1. open solution.
  2. right-click on project you want to compile into exe -> Properties.
  3. Configuration Properties -> General.
  4. In Project Defaults change Configuration Type to Application(.exe)

Is a DLL compiled code? ›

A . dll file contains compiled code you can use in your application.

How do I decode a DLL file? ›

In order to see the code that makes a DLL file work, you will need to use a decompiler to revert it back into readable code. Opening a DLL file without a decompiler (such as opening it with Notepad) will result in a jumbled mess of unreadable characters. dotPeek is one of the more popular free decompilers.

How to compile source code Visual Studio? ›

Create a New Project

Enter a name and location for your project and select 'Create'. Visual Studio will then generate the necessary project files, and you can add code to your project. Once your project is set up, you can compile your code by pressing the “Build” button.

How to add DLL to Visual Studio code? ›

Simply #include the DLL header files in your C++ code, and then add the DLL import . lib files to your Project. Or, you can load the DLL functions dynamically at runtime via GetProcAddress() if you don't have . lib files (though you can easily generate them from the DLLs if needed).

What is the difference between .EXE and .DLL files? ›

The EXE will create its own thread and reserve resources for it if you run it. A DLL file, on the other hand, is an in-process server, so you cannot run a DLL file on its own.
...
Difference between EXE and DLL.
Basis of ComparisonEXEDLL
TypeAn exe is a programDLL is a library
5 more rows
Aug 4, 2022

Is a DLL file an EXE file? ›

A DLL file is not by it self executable, though it may contain executable code. A DLL (Dynamic-Link Library) contains code, data, resources etc. usable by other programs. You need an EXE file for the operating system to execute code within DLL files, like "RUNDLL.

What is DLL file extension? ›

A dynamic link library (DLL) is a collection of small programs that larger programs can load when needed to complete specific tasks. The small program, called a DLL file, contains instructions that help the larger program handle what may not be a core function of the original program.

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5592

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.