writing php extension that uses external shared library

I have difficulty trying to write a php extension that uses an external shared library:

The content of config.m4 is as follows:

PHP_ARG_WITH([foo],
  [for foo support],
  [AS_HELP_STRING([--with-foo],
    [Include foo support])])

if test "$PHP_FOO" != "no"; then
  PKG_CHECK_MODULES([LIBFREETYPE2], [freetype2 >= 23.4.17])
  PHP_EVAL_INCLINE($LIBFREETYPE2_CFLAGS)
  PHP_EVAL_LIBLINE($LIBFREETYPE2_LIBS, FOO_SHARED_LIBADD)
  PHP_NEW_EXTENSION(foo, foo.c, $ext_shared)
fi

The code of the main function in foo.c is as follows(Just a very simple example of using the external FreeType library):

PHP_FUNCTION(foo_test) {
    ZEND_PARSE_PARAMETERS_NONE();

    FT_Library ft;

    if (FT_Init_FreeType(&ft)) {
        php_printf("FreeType library failed to load\n");
        return;
    }
    php_printf("FreeType library successfully loaded\n");

    FT_Done_Library(ft);
    php_printf("FreeType library unloaded\n");
}

phpize, ./configure, make and make install all succeeded, and then I added the extension in php.ini:

extension = foo.so

So far so good, but then I ran the following command

echo "<?php foo_test();" | php

I got the following error message:

php: symbol lookup error: /the-path-of-foo/foo.so: undefined symbol: FT_Init_FreeType

It looks like PHP doesn't load the external shared FreeType library my PHP extension intends to use.

How do I do to make PHP load the external shared FreeType library?

Thanks for help.

Answer

Solution:

Resolved this issue by adding PHP_SUBST(FOO_SHARED_LIBADD) after PHP_EVAL_LIBLINE($LIBFREETYPE2_LIBS, FOO_SHARED_LIBADD):

PHP_ARG_WITH([foo],
  [for foo support],
  [AS_HELP_STRING([--with-foo],
    [Include foo support])])

if test "$PHP_FOO" != "no"; then
  PKG_CHECK_MODULES([LIBFREETYPE2], [freetype2 >= 23.4.17])
  PHP_EVAL_INCLINE($LIBFREETYPE2_CFLAGS)
  PHP_EVAL_LIBLINE($LIBFREETYPE2_LIBS, FOO_SHARED_LIBADD)
  PHP_SUBST(FOO_SHARED_LIBADD)
  PHP_NEW_EXTENSION(foo, foo.c, $ext_shared)
fi

Source