Tuesday, August 24, 2010

Linking C libraries to php extensions

After learning to create a simple php module it took a long time for me to understand the correct way of porting an existing C library to to a php module. Sometimes I thought it may be impossible for me. After all I have managed to understand the correct way of doing it. If you need to learn about creating a simple php module, follow this article. So, here's the procedure I followed to build the php module which is linked to a C library.

1) First step is creating a simple C library to link to a php module. So, write and save the following source files.

Here's the "asanka.c" file,

#include "asanka.h"

void setValue(int a){
value = a;

int getValue(){
return value;

void increment(){
value = value + 1;

void decrement(){
value = value - 1;

Here's the "asanka.h" file,


int value;

void setValue(int a);

int getValue();

void increment();

void decrement();

Now create the C library called "libasanka.a" by giving the command,

gcc -c asanka.c
ar -cr libasanka.a asanka.o

Ok, now we have the library. Create a directory in some place, lets's say in your home directory as the "library". Inside that directory, create two directories as "lib" and "include". Put the library file "libasanka.a" into the "lib" directory and "asanka.h" file to the "include" directory. So, now the paths to both should be,


There's a reason for giving the path's of the files. We have to give the path when we create the php module.

2) Now it's time to create the php module. Goto ext directory of the php source and give the command in the terminal,

./ext_skel --extname=asanka

3) Now go into the newly created directory "asanka" inside the ext directory and open the auto generated file "asanka.c" (this is not the file we created). Add the following entries in the appropriated places,

PHP_FE(asanka_setValue, NULL)
PHP_FE(asanka_getValue, NULL)
PHP_FE(asanka_increment, NULL)
PHP_FE(asanka_decrement, NULL)

Add the following entries also in the appropriate place in the file,

int locValue;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc TSRMLS_CC, "l", &locValue) == FAILURE) {
php_error_doc_ref(NULL TSRMLS_CC, E_ERROR, "Invalid Parameters");


int locValue;
locValue = getValue();



4) Now open the file "php_asanka.h" in the same directory and add the following to it,


5) Open the config.m4 file and add change it's content to be match with the following code,

dnl $Id$
dnl config.m4 for extension asanka

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

PHP_ARG_WITH(asanka, for asanka support,
[ --with-asanka[=DIR] Include asanka support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(asanka, whether to enable asanka support,
dnl Make sure that the comment is aligned:
dnl [ --enable-asanka Enable asanka support])

if test "$PHP_ASANKA" != "no"; then
dnl Write more examples of tests here...

dnl # --with-asanka -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/asanka.h" # you most likely want to change this
if test -r $PHP_ASANKA/lib/libasanka.a; then # path given as parameter
else # search default path list
AC_MSG_CHECKING([for asanka files in default path])
for i in /usr/local /usr; do
if test -r $i/lib/libasanka.a; then
AC_MSG_RESULT(found in $i)
if test -z "$ASANKA_DIR"; then
AC_MSG_RESULT([not found])
AC_MSG_ERROR([Please reinstall the libasanka distribution - asanka.h should be /include and libasanka.a should be in /lib])

dnl # --with-asanka -> add include path

dnl # --with-asanka -> check for lib and symbol presence
LIBNAME=asanka # you may want to change this
LIBSYMBOL=getValue # you most likely want to change this

AC_MSG_ERROR([wrong asanka lib version or lib not found])
-L$ASANKA_DIR/lib -lm

PHP_NEW_EXTENSION(asanka, asanka.c, $ext_shared)

6) Now run the following commands in terminal inside the "asanka" directory,


./configure --with-asanka=/home/username/library/

sudo make

7) Now our new module is ready. You can find it in the "modules" directory inside the "asanka" directory. Copy that "asanka.so" shared object to the extensions directory of your installed php. If you don't know the path to the extensions directory, look at the phpinfo() page.

8) After placing "asanka.so" into the extensions directory, next thing is editing the php.ini file. Open it and add an entry as follows in the appropriate place,


9) Now every things ok. Write a php file with the following content and place it in the "htdocs" directory of the apache server,

echo asanka_getValue();

Check whether your web browser displays the correct output. If you see number 12, then your C library works correctly with the php module.


  1. Great!
    I have a c++ Dynamic Shared Library (.so) which I want to use within my php code. can I use this Code, too