gpt4 book ai didi

PHP 链接到扩展中的共享库

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:44 27 4
gpt4 key购买 nike

我正在开发一个 native PHP 扩展,其中包含来自共享库的符号,但我无法获取 config.m4 文件来生成包含必要的链接器选项的构建过程正确链接到共享库。到目前为止,这是我的 config.m4 文件中的内容:

PHP_ARG_ENABLE(myextension, whether to enable MyExtension support, [ --enable-myextension   Enable MyExtension support])

if test "$PHP_MYEXTENSION" = "yes"; then

LIBNAME=otherlibrary
LIBSYMBOL=otherlib_some_function
LIBOTHERLIB_LIBS=`pkg-config --libs otherlibrary`
LIBOTHERLIB_INC=`pkg-config --cflags otherlibrary`

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
[
PHP_EVAL_INCLINE($LIBOTHERLIB_INC)
PHP_EVAL_LIBLINE($LIBOTHERLIB_LIBS)
AC_DEFINE(HAVE_MYEXTENSION, 1, [Whether you have MyExtension])
],[
AC_MSG_ERROR([wrong lib$LIBNAME version or library not found])
])

PHP_NEW_EXTENSION(myextension, myextension.c, $ext_shared)
fi

libotherlibrary 库位于标准系统位置。当我使用此配置进行 phpize,然后使用 --enable-myextension 参数执行生成的 configure 脚本时,它成功生成了一个 Makefile 等,我可以看到它对 libotherlibrary 中的 otherlib_some_function 进行了测试,该测试通过了。但是在我生成的 myextension.so 上调用 ldd 只给出:

linux-vdso.so.1 (0x00007ffd11fce000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f741e653000)
/lib64/ld-linux-x86-64.so.2 (0x00007f741ec26000)

所以没有提到 libotherlibraryMakefile 执行的libtoolcc 命令不包含-lotherlibrary 选项。然后当我尝试执行一个使用我的扩展的 PHP 脚本时,我得到:

php: symbol lookup error: /path/to/modules/myext.so: undefined symbol: otherlib_some_function

代替 PHP_EVAL_INCLINEPHP_EVAL_LIBLINE 我还尝试了 PHP_ADD_LIBRARY 宏:

PHP_ADD_LIBRARY($LIBNAME,,OTHERLIB_SHARED_LIBADD)

(我不明白这个宏的第三个参数是干什么用的。)

我也尝试过(结合以上两种方法),包括传递给 PHP_CHECK_LIBRARY 的第五个参数,称为 extra-libs,传入我的 $LIBOTHERLIB_LIBS 变量。这样做并不能解决任何问题,也没有关于该参数应该做什么或它需要什么值的文档。所以这真的只是一个猜测。

那么我怎样才能使用所需的 -lotherlibrary 链接器选项构建我的扩展?

最佳答案

您需要使用 PHP_ARG_WITH 而不是 PHP_ARG_ENABLE + 加上一些其他更改。这是一个对我有用的例子。将 libxyz 替换为您的外部库的名称:

config.m4

dnl If your extension references something external, use with:

PHP_ARG_WITH(libxyz, for libxyz support,
Make sure that the comment is aligned:
[ --with-libxyz Include libxyz support])

dnl Otherwise use enable:

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

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

dnl # --with-libxyz -> check with-path
SEARCH_PATH="/usr/local /usr"
SEARCH_FOR="/include/libxyz.h"
if test -r $PHP_LIBXYZ/$SEARCH_FOR; then # path given as parameter
LIBXYZ_DIR=$PHP_LIBXYZ
else # search default path list
AC_MSG_CHECKING([for libxyz files in default path])
for i in $SEARCH_PATH ; do
if test -r $i/$SEARCH_FOR; then
LIBXYZ_DIR=$i
AC_MSG_RESULT(found in $i)
fi
done
fi
dnl
if test -z "$LIBXYZ_DIR"; then
AC_MSG_RESULT([not found])
AC_MSG_ERROR([Please reinstall the libxyz distribution])
fi

dnl # --with-libxyz -> add include path
PHP_ADD_INCLUDE($LIBXYZ_DIR/include)

dnl # --with-libxyz -> check for lib and symbol presence
LIBNAME=libxyz
LIBSYMBOL=libxyz_lib_version

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
[
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $LIBXYZ_DIR/lib, LIBXYZ_SHARED_LIBADD)
AC_DEFINE(HAVE_LIBXYZLIB,1,[ ])
],[
AC_MSG_ERROR([wrong libxyz lib version or lib not found])
],[
-L$LIBXYZ_DIR/lib -lm
])

PHP_SUBST(LIBXYZ_SHARED_LIBADD)

PHP_NEW_EXTENSION(libxyz, libxyz.c, $ext_shared)
fi

检查这个:http://php.net/manual/de/internals2.buildsys.configunix.php

关于PHP 链接到扩展中的共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com