使用 libtidy 的 PHP 扩展可以编译,但无法加载

发布于 2024-09-04 13:43:37 字数 2764 浏览 8 评论 0原文

我用 C++ 编写了一个使用 libtidy 的扩展,当我编译 PHP --with-tidy 时,它可以在 PHP 下完美运行。

然而,如果该扩展能够在普通 PHP 上运行,那就太好了。当我尝试使用扩展程序时,我得到类似以下信息:

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

并且扩展程序未加载。

显然,官方的整洁扩展工作得很好。我在系统上安装了相关的libtidy开发包,并且编译+链接没有问题。我试图查看整洁的扩展的代码,但它是大量的宏 - 随机复制片段感觉就像货物代码。

除了使用 PHP_ADD_LIBRARY_WITH_PATH(tidy, $TIDY_LIBDIR, TIDY_SHARED_LIBADD) 链接到库之外,是否有 PHP 扩展或 C 语句可以修复此错误?

提前致谢!!

编辑:这是整个 config.m4 文件:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-09-11 13:43:37

你是否使用与普通 php 相同的 php 版本来编译你的扩展?您应该使用相同版本的 php 源。
此外,请确保在 php.ini 文件中,您在扩展名之前加载了“libtidy”扩展名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文