Reputation: 11
I was given a libxxx.so
file which i can't view it's source code and i can compile my c file with the next command:
gcc -ldl libxxx.so my.c -a a.out
a.out
worked well in command line. But after i wrapped it into a PHP
extension, PHP
showed a message like this:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/myextension.so' - /usr/lib64/php/modules/myextension.so: undefined symbol: xxxfunction in Unknown on line 0
xxxfunction is a function exported by libxxx.so
.
I'm new to C
and I think the problem is the way of compiling my PHP
extension, i searched google but didn't find the answer.
I'm wondering whether there is an option like -ldl in gcc so i can specify that i'm going to use the libxxx.so in my PHP
extention.
I compiled my extension by next steps:
cd myextension/
phpize
./configure
make
make install
I have moved libxxx.so
file to /usr/local/lib
.
My /etc/ld.so.conf:
include ld.so.conf.d/*.conf
/usr/local/lib
I have runed lddconfig
.
Upvotes: 0
Views: 753
Reputation: 871
You need to tell the build system to link against the shared library at compile time. This allows the dependency to be stored in the compiled PHP extension, where it will be loaded at first use.
To do this, you need to modify the config.m4
to add a library. You can do this via the PHP_ADD_LIBRARY
macro. This macro adds a library to the link line and has the form PHP_ADD_LIBRARY($name,$append,$var)
. The first parameter is the name of the library; this is the name of the *.so
file without the lib
prefix (e.g. xxx
). The second parameter determines (I presume) whether the line is appended or overwritten; this should usually be set to 1
. The final parameter is the variable name into which the line is stored. This variable must be substituted into the output file via PHP_SUBST
.
You should place your PHP_ADD_LIBRARY
line somewhere before the PHP_NEW_EXTENSION
macro in the config.m4
file. In addition, it's a good idea to wrap the PHP_ADD_LIBRARY
in a PHP_CHECK_LIBRARY
call. This ensures the library can be found by the build system before adding it. See my example below.
Here is another SO question that is similar to yours. It has some working examples you can follow. Additionally, I'll include a quick example here:
PHP_CHECK_LIBRARY(xxx,xxxfunction,[
PHP_ADD_LIBRARY(xxx,1,XXX_SHARED_LIBADD)
],[
AC_MSG_ERROR([Please install libxxx on the system])
],
[])
PHP_SUBST([XXX_SHARED_LIBADD])
Note: The second parameter to PHP_CHECK_LIBRARY
is a function for which the build system will check if it finds the given library.
Upvotes: 2