Home » Php » linux – PHP Extension using libtidy compiles, but does not load

linux – PHP Extension using libtidy compiles, but does not load

Posted by: admin July 12, 2020 Leave a comment

Questions:

I wrote an extension in C++ that uses libtidy, and it runs perfectly under PHP when I compile PHP --with-tidy.

However, it would be nice to have the extension run on a vanilla PHP. When I try to use the extension, I get something like:

PHP Warning:   
  PHP Startup: 
    Unable to load dynamic library 'extension.so': 
      undefined symbol: tidyCleanAndRepair in Unknown on line 0

and the extension is not loaded.

Obviously, the official tidy extension works fine. I have the relevant libtidy development packages installed on the system, and it compiles+links without a problem. I have tried to look through the code for the tidy extension, but it is a huge mass of macros – copying pieces at random felt like cargo code.

Besides linking to the library with PHP_ADD_LIBRARY_WITH_PATH(tidy, $TIDY_LIBDIR, TIDY_SHARED_LIBADD), Is there a PHP extension or C statement that fixes this error?

Thanks in advance!!

EDIT: here is the entire config.m4 file:

dnl config.m4 for extension htmlparser

PHP_ARG_ENABLE(htmlparse, whether to enable htmlparser support,
 [  --enable-htmlparser           Enable htmlparser support])

if test "$PHP_HTMLPARSER" != "no"; then


  if test -r $PHP_LIBXML2/lib/libxml2.a; then
    LIBXML2_DIR=$PHP_LIBXML2
  else
    AC_MSG_CHECKING(for libxml2 in default path)
    for i in /usr/local /usr; do
      if test -r $i/lib/libxml2.a; then
        LIBXML2_DIR=$i
        AC_MSG_RESULT(found in $i)
      fi
    done
  fi

  if test -z "$LIBXML2_DIR"; then
    AC_MSG_RESULT(not found)
    AC_MSG_ERROR(Please reinstall the libxml2 distribution - libxml2.h should
                 be in <libxml2-dir>/include and libxml2.a should be in <libxml2-    dir>/lib)
  fi
  PHP_ADD_INCLUDE($LIBXML2_DIR/include/libxml2)
  PHP_ADD_LIBRARY_WITH_PATH(libxml2, $LIBXML2_DIR/lib, LIBXML2_SHARED_LIBADD)

  AC_MSG_CHECKING(for boost in default path)
  for i in /usr/local /usr; do
    if test -r $i/include/boost; then
      BOOST_DIR=$i
      AC_MSG_RESULT(found in $i)
    fi
  done

  if test -z "$BOOST_DIR"; then
    AC_MSG_RESULT(not found)
    AC_MSG_ERROR(Please reinstall the boost distribution!!!)
  fi
  PHP_ADD_INCLUDE($BOOST_DIR/include/boost/)


    TIDY_SEARCH_DIRS="/usr/local /usr"
    for i in $TIDY_SEARCH_DIRS; do
        if test -f $i/include/tidy/tidy.h; then
            TIDY_DIR=$i
            TIDY_INCDIR=$i/include/tidy
        elif test -f $i/include/tidy.h; then
            TIDY_DIR=$i
            TIDY_INCDIR=$i/include
        fi
    done


    if test -z "$TIDY_DIR"; then
        AC_MSG_ERROR(Cannot find libtidy)
    fi

  TIDY_LIBDIR=$TIDY_DIR/lib

  PHP_ADD_INCLUDE($TIDY_INCDIR)
  PHP_ADD_LIBRARY_WITH_PATH(tidy, $TIDY_LIBDIR, TIDY_SHARED_LIBADD)

  PHP_CHECK_LIBRARY(tidy,tidyOptGetDoc,
  [
  AC_DEFINE(HAVE_TIDYOPTGETDOC,1,[ ])
  ],[],[])

  AC_DEFINE(HAVE_HTMLPARSER, 1, [Whether you want htmlparser support])
  PHP_SUBST(HTMLPARSER_SHARED_LIBADD)
  PHP_ADD_LIBRARY_WITH_PATH(stdc++, 1, HTMLPARSER_SHARED_LIBADD)
  PHP_REQUIRE_CXX()
  PHP_NEW_EXTENSION(htmlparser, php_htmlparser.cpp parsehtml.cpp StringBuilder.cpp,     $ext_shared)
fi
How to&Answers:

did you compile your extension with the same php version as a vanilla php? you should use php sources with the same version.
besides, make sure that in php.ini file you loaded ‘libtidy’ extension before your extension.