PHP extension development with Linux and Eclipse CDT. Building your environment
It is hard to start with PHP extensions development in Unix environment. It is not enough to be familiar with C programming language also you need at least basic knowledge in packaging, GNU build tools and of course PHP internals. If your desktop is beautified by Linux - there is a good news for you: you don't need to setup and build everything by yourself! You just need to know how to use already configured package for your distribution. Don't even ask what to do if your desktop is crippled by Windows - play minesweeper.
This post is intended to give you a good place to start.
Everything is shown on example of the Opensuse 11.4, but other distributions are suitable as well.
First of all we need to obtain PHP source code. Raw PHP source from php.net can be used but you will need to take care of PHP configuring and all external symbols.
Delete stripped php binaries first:
# zypper rm php5
next
# zypper si php5
this command will download source RPM package and take care of all external depencies (development headers) required to build PHP from source.
Open /usr/src/packages/SPECS/php5.spec with your favorite text editor and replace --disable-debug with --enable-debug. This is instruction to php configure script to take care of building non-stripped binaries.
Next command will build PHP and create binary PHP packages containing debug symbols.
# rpmbuild -ba /usr/src/packages/SPECS/php5.spec
Next install php from binary packages you just built:
# zypper in /usr/src/packages/RPMS/YOUR_ARCHITECTURE/php5-5.3.5-5.16.1.x86_64.rpm
And required extensions as well (i prefer something like this ls -1 | xargs zypper in)
Check whether you made no mistakes and PHP binary contains debug information (not stripped)
# file /usr/bin/php5
Should show something like this:
/usr/bin/php5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped
Create new Eclipse CDT project File\New\Make file project with existing code and enter settings like on the screenshot:
Note: CDT should be able to write to PHP source directory so it is better to change recursively directory owner to the same user as CDT running from.
Next add build configuration for the CLI SAPI: Project\Properties\C/C++ Build\Manage configurations\New
Press "Set Active" button, change build location to ${workspace_loc:/php-5.3.5/build-cli}
Open sapi/cli/php_cli.c and you can start a debug session using Run\Debug As\Local C/C++ Application CDT will run make command in build_cli directory automatically for you each time you start debugging.
To fix wrong GDB source lookup (symptoms are broken breakpoints, "no such file" GDB errors) open Run\Debug configurations\php\Source and delete "Default" source lookup path and add "Absolute File Path" instead. Recently i was setting up this environment in Opensuse 12.2 and PHP 5.3.22 and this step is necessary to make debugging work.
You can debug other SAPIs as well by adding new build configurations.
Follow-up: how to develop basic extension http://cmyker.blogspot.com/2013/03/php-extension-development-with-linux.html
This post is intended to give you a good place to start.
Everything is shown on example of the Opensuse 11.4, but other distributions are suitable as well.
First of all we need to obtain PHP source code. Raw PHP source from php.net can be used but you will need to take care of PHP configuring and all external symbols.
Delete stripped php binaries first:
# zypper rm php5
next
# zypper si php5
this command will download source RPM package and take care of all external depencies (development headers) required to build PHP from source.
Open /usr/src/packages/SPECS/php5.spec with your favorite text editor and replace --disable-debug with --enable-debug. This is instruction to php configure script to take care of building non-stripped binaries.
Next command will build PHP and create binary PHP packages containing debug symbols.
# rpmbuild -ba /usr/src/packages/SPECS/php5.spec
Next install php from binary packages you just built:
# zypper in /usr/src/packages/RPMS/YOUR_ARCHITECTURE/php5-5.3.5-5.16.1.x86_64.rpm
And required extensions as well (i prefer something like this ls -1 | xargs zypper in)
Check whether you made no mistakes and PHP binary contains debug information (not stripped)
# file /usr/bin/php5
Should show something like this:
/usr/bin/php5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped
Create new Eclipse CDT project File\New\Make file project with existing code and enter settings like on the screenshot:
Note: CDT should be able to write to PHP source directory so it is better to change recursively directory owner to the same user as CDT running from.
Next add build configuration for the CLI SAPI: Project\Properties\C/C++ Build\Manage configurations\New
Press "Set Active" button, change build location to ${workspace_loc:/php-5.3.5/build-cli}
Open sapi/cli/php_cli.c and you can start a debug session using Run\Debug As\Local C/C++ Application CDT will run make command in build_cli directory automatically for you each time you start debugging.
To fix wrong GDB source lookup (symptoms are broken breakpoints, "no such file" GDB errors) open Run\Debug configurations\php\Source and delete "Default" source lookup path and add "Absolute File Path" instead. Recently i was setting up this environment in Opensuse 12.2 and PHP 5.3.22 and this step is necessary to make debugging work.
You can debug other SAPIs as well by adding new build configurations.
Follow-up: how to develop basic extension http://cmyker.blogspot.com/2013/03/php-extension-development-with-linux.html
hi,
ReplyDeletesorry if they may seem stupid questions: I'm no C developer, and haven't coded in C in a while, and even then i haven't worked in big C project like 'php'.
So here they are:
1) What do you mean by
'Raw PHP source from php.net can be used but you will need to take care of PHP configuring and all external symbols.'
2)php cli will be rebuilt everytime you want to test some new C code added? Won't that take a long time? this is something i just can't understand.....
i'm going to try this now anyway....
1) You need to configure PHP for your OS (things like binaries and .so extensions location) Under external symbols i mean linking (with GCC C Linker) against external functions from external libraries. You need development headers of all external libraries used by PHP. Read this article for more details http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html
ReplyDelete2) GNU Make is smart enough to rebuild only changed part of the binary. C binary consists of 1 or more .o files (compiled part of the code, typically .h and .c files with the same name). Linker takes those .o files and assembles binary file. If you changed only 1 .c file only 1 .o file will be recompiled (recompiling necessity is determining by .o file and .c or .h files modify time comparison). This process is very fast and shouldn’t bother you.
great, thanks for the help man!:)
ReplyDeleteI feel such like an old man.....like rocky coming back in his 60's :))