Developing Custom PHP Extensions: Part 3 - Setting up our Development environment

In this tutorial I will use Microsoft's Visual C++ 6.0 compiler. Compiling extensions on Unix / Linux systems is fairly well documented. On windows, however, this isn't the case.

The first thing you must do is download the PHP source code distribution, php-4.2.1.tar.gz, from the PHP web site.

At the time of writing the latest version of PHP was 4.2.1

Once the source code is downloaded, create a directory where you will keep the source code and where you will hold the sources of your extensions. In my case, this will be D:\savents\php-dev\php-4.2.1\. Extract the contents of php-4.2.1.tar.gz into that directory (I used WinRAR to extract the file).

Next, move into the \ext directory, which is D:\savents\php-dev\php-4.2.1\ext\ in my case. There you will see all of the different extensions that are currently being distributed with PHP. Any time during your coding process when something does not work, it is a good idea to just open an extension and read through the source code.

Moving right along, open your MS Visual Studio and make sure no other projects or files are open. Under the File menu, choose New and from the list of wizards choose the "Win32 Dynamic-Link Library". All PHP extensions under windows must be in form of a DLL, thus it is imperative to choose the right type of project. Choosing anything different will result in ugly and indecipherable linker errors (If you've ever worked with Visual C++, no doubt you are familiar with the cryptic messages the compiler throws at you).

Now, for the location. Enter – or rather browse to – the extension subdirectory in your PHP development directory. For the project name enter "savents" and this should be appended automatically to your location.

Click the OK button. On the next menu choose "An empty DLL project" and click the finish button. A new window should open up and show you all the information relating to the project; click the OK button again.

Now, the following few steps must, unfortunately, be completed for each extension project you create:

1. Under the Build menu select Configuration (Build->Configuration). In that dialog, add two configurations: Release_TS and Debug_TS. Make sure Release_TS copies the settings from the Release configuration and Debug_TS copies the setting from the Debug configuration. After this is done, remove the Release and Debug configurations. Make sure the dialog looks as follows and close it:
2. Now we will set the options in the Release_TS configuration. Open up Project > Settings. On the C/C++ tab, General Category, add the following preprocessor definitions: ZEND_DEBUG=0, COMPILE_DL_YOUR_EXTENSION_NAME, ZTS=1, ZEND_WIN32, PHP_WIN32. YOUR_EXTENSION_NAME is the name of your extension. For example: COMPILE_DL_SAVENTS_MOD. 

3. In the Code Generation Category, change Use run-time Library to "Multithreaded DLL". 

4. In the Preprocessor Category, add the following "Additional include directories": ..\.., ..\..\main, ..\..\Zend, ..\..\TSRM, ..\..\bindlib_w32. 

The directories are relative to where your project is found. Since our project lives in the \ext directory, the paths are right. However, if you plan to hold your extensions elsewhere you will need to change the directories appropriately.

5. On the Link tab in the General Category, change the Output file name to ..\..\Release_TS/php_saventsmod.dll . In future projects you will be able to change the name to anything (*.dll). However, most PHP extensions come with the prefix php_ to indicate they are in-fact PHP extensions. (php_*.dll) 

6. On the same tab (Link), same category (General), add php4ts.lib to Object/library modules. 

7. In the Input Category, add the following to Additional library path: ..\..\Release_TS. 

Close the dialog and set the active configuration to Release_TS. This is done by the Build->Set Active Configuration... dialog. 

Our development environment is almost set – or it is completely set, depending on what you do next. You see, PHP was written in C and not C++, thus all extensions must be written in such a way that they use the C naming convention. Using C++ and not C would cause a linker error (LNK2001 to be exact). 

When you add a new source code file to your project, the default extension (not to be mixed with our topic) is *.CPP. This extension causes the compiler to use the C++ naming convention and ultimately leads to the linker error. You can avoid this by changing the extension on the source code file to .C, which will force to compiler to use the C naming convention. On the other hand, if you prefer to continue using .CPP files you will need to add the following option in your project options: /Tc 

This will force to compiler to use C naming convention even though it is a C++ source code file. 

0 comments:

Post a Comment