PHP链接到扩展中的共享库 [英] PHP Linking to a shared library in an extension

查看:95
本文介绍了PHP链接到扩展中的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本机PHP扩展,其中包括共享库中的符号,但是我无法获取config.m4文件来生成包含必要链接程序选项的构建过程,以正确链接到共享库.到目前为止,这是我的config.m4文件中的内容:

I'm working on a native PHP extension which includes symbols from a shared library, but I can't get the config.m4 file to generate a build procedure that includes the necessary linker options to correctly link to the share library. This is what I have in my config.m4 file so far:

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等.我可以看到它正在通过libotherlibraryotherlib_some_function进行测试.但是,在生成的myextension.so上调用ldd只会得到:

The libotherlibrary library is in a standard system location. When I phpize with this configuration and then execute the generated configure script with the --enable-myextension argument it successfully generates a Makefile etc. and I can see it doing the test for otherlib_some_function from libotherlibrary which passes. But calling ldd on my resulting myextension.so gives just:

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)

,所以没有提及libotherlibrary.并且Makefile执行的libtoolcc命令不包含-lotherlibrary选项.然后,当我尝试执行使用扩展名的PHP脚本时,我得到:

so no mention of libotherlibrary. And the libtool and cc commands which are executed by the Makefile don't include the -lotherlibrary option. And then when I try and execute a PHP script which uses my extension I get:

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

代替PHP_EVAL_INCLINEPHP_EVAL_LIBLINE,我也尝试了PHP_ADD_LIBRARY宏:

In place of PHP_EVAL_INCLINE and PHP_EVAL_LIBLINE I've also tried the PHP_ADD_LIBRARY macro:

PHP_ADD_LIBRARY($LIBNAME,,OTHERLIB_SHARED_LIBADD)

(我不明白该宏的第三个参数是什么.)

(I don't understand what the third argument to this macro is for.)

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

I've also tried (in combination with both of the above), including the fifth argument to PHP_CHECK_LIBRARY called extra-libs passing in the value of my $LIBOTHERLIB_LIBS variable. Doing so doesn't fix anything, and there's no documentation of what this argument is supposed to do or what values it requires. So this is just a guess really.

那么,如何使用所需的-lotherlibrary链接器选项来构建扩展程序?

So how can I get my extension to build with the required -lotherlibrary linker option?

推荐答案

我的工作版本如下:

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_ADD_LIBRARY($LIBNAME,1,OTHERLIBRARY_SHARED_LIBADD)
    PHP_SUBST(OTHERLIBRARY_SHARED_LIBADD)
    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

该技术使用pkg-config查找共享库,因此它可能不像@ hek2mgl的显式搜索那样具有可移植性.

This technique uses pkg-config to find the shared library so perhaps it may not be as portable as @hek2mgl's explicit searching.

主要添加的功能是使用PHP_SUBST宏,该宏将具有其值的变量添加到Makefile中"(根据phpize创建的aclocal.m4).似乎记录为将库添加到链接行"的宏PHP_ADD_LIBRARY实际上并没有向Makefile添加任何内容.而是提供一个变量名作为其第三个参数(记录为"shared-libadd"),然后调用PHP_SUBSTMakefile中更新该变量.

The main addition is the use of the PHP_SUBST macro which "Adds variable with it's value into Makefile" (according to the aclocal.m4 that phpize created). It seems that the macro PHP_ADD_LIBRARY, documented as "add a library to the link line", doesn't actually add anything to your Makefile. Instead, you supply a variable name as its third argument (documented as "shared-libadd") and later call PHP_SUBST to update that variable in the Makefile.

这篇关于PHP链接到扩展中的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆