Shared library nesting dependency problem under "original" Linux

Source: Internet
Author: User


Problem Scenario:
    1. Dynamic Library librabbitmq_r.so internally relies on dynamic library libevent_core.so and libevent_pthreads.so;
    2. The executable SA relies on dynamic library librabbitmq_r.so;
    3. when the link is delivered to SA, you want to specify only librabbitmq_r.so without specifying libevent_core.so and libevent_pthreads.so.
error message:
... g++. /SOURCE/AUTHORISECFG.O. /SOURCE/BMCINST.O. /SOURCE/CONFIG.O. /SOURCE/LGSINST.O. /SOURCE/LOGICSRV.O. /SOURCE/LOGICSRVINST.O. /SOURCE/LOGICSRVMODULELISTCFG.O. /SOURCE/MAIN.O. /SOURCE/MODULEINST.O. /SOURCE/PRINT.O. /SOURCE/ROUTINGKEYCFG.O. /SOURCE/SAUTILS.O. /SOURCE/STRUCTSELF.O. /source/. /.. /.. /COMMON/SOURCE/BOSSUTILS.O. /source/. /.. /.. /common/source/bossversion.o-o sa-m32-l. /.. /.. /.. /10-common/lib/release/linux-lrt-lwatchdogclient-lfiletransfer-losp-lkprop-ljsonconvert-ljsoncpp-ldeploycfg- Lnosectionini-lrabbitmq_r-lmqwrapper-lreadwritelock-lcaptureexception-lnetconfig/usr/bin/ld:warning:libevent_ Core.so, needed by. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so, not found (try Using-rpath or-rpath-link)/usr/bin/ld:warning: Libevent_pthreads.so, needed by. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so, not found (try Using-rpath or-rpath-link): /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined ReferenceTo ' Event_base_free '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' evthread_use_pthreads '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' event_assign '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' Event_base_dispatch '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' Event_base_loopbreak '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' Event_del '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' Event_add '. /.. /.. /.. /10-common/lib/release/linux/librabbitmq_r.so:undefined reference to ' Event_base_new ' Collect2:ld returned 1 exit Statusmake: * * * [SA] Error 1
as you can see from the error message, the symbols that are not found are within libevent_core.so and libevent_pthreads.so. But these two libraries do exist in-L. /.. /.. /.. /10-common/lib/release/linux path, but why is the linker still unable to find the corresponding libraries and symbols?

The following article discusses this issue (some excerpts are given below)

Why does LD Need-rpath-link when linking an executable against a so, needs another so? "

...
You system, through ld.so.conf, LD.SO.CONF.D, and the system environment, Ld_library_path, etc:, provides the system- Wide Library search paths which is supplemented by installed libraries through pkg-config information and the as when Y OU build against standard libraries.
...
there is no standard Run-time library search path for custom GKFX libraries you create yourself. You specify the search path to your libraries through The-l/path/to/lib designation during compile and link. For libraries in non-standard locations, the library search path can is optionally placed in the header of your executable (ELF header) at Compile-time so, your executable can find the needed libraries.
...
Rpath provides a by embedding your custom Run-time library search path in the ELF header so that your custom Libra Ries can found as well without have to specify the search path each time it is used. This applies to libraries the depend on libraries as well. As you had found, not only was the order specify the libraries on the command line important, you also must provide th e run-time Library Search path, or rpath, information for each dependent library is linking against as well so that T He header contains the location of any libraries needed to run.
...
Back to the semantics of LD. In order to produce a "good link", LD must is able to locate all dependent libraries. LD cannot insure a good link otherwise. The runtime linker must find and load, not just to find the GKFX libraries needed by a program. LD cannot guarantee that would happen unless LD itself can locate all needed GKFX libraries at the time of the Progam is Lin ked.
...

The conclusion is that, like this a.so relies on b.so, and C relies on a.so, it is necessary to specify the location of the required. So in the link process, rather than just specifying with- l .

examples are as follows:

[[email protected] include_test]# ll total dosage 20-rw-r--r--1 root root 149 September  16:19 main.c-rw-r--r--1 root root 123 September 
   14 16:40 say_hello.c-rw-r--r--1 root root  20 September  15:58 say_hello.h-rw-r--r--1 root root 416 September  14 16:39 time_print.c-rw-r--r--1 root root  69 September  15:00 time_print.h[[email protected] include_test]#

"SAY_HELLO.C"
g++-o say_hello.so-fpic-shared say_hello.c#include <stdio.h>void Say_hello () {    printf ("Hello world!\n");}
"Say_hello.h"
void Say_hello ();
"Time_print.c"
g++-O time_print.so-fpic-shared-i.-l time_print.c say_hello.so #include <time_print.h> #include <stdio.h& gt; #include <say_hello.h>int time_print (time_t tmp) {    int off = 0;    time_t T;    Char buf[64] = {0};    T = time (NULL);    Off = strftime (buf, sizeof (BUF), "%d%b%h:%m:%s", LocalTime (&t));    fprintf (stderr, "current timestamp =%s\n", buf);    Say_hello ();    return 0;}
"Time_print.h"
#include <time.h>int time_print (time_t t);
"MAIN.C"
g++-o main main.c time_print.so-i.-wl,-rpath-link,. #include <time_print.h>int main () {    time_t t;    Time_print (t);    return 0;}

generate two. So libraries
[[email protected] include_test]# g++-o say_hello.so-fpic-shared say_hello.c[[email protected] Include_ test]# ll total dosage 28-rw-r--r--1 root root 149 September 16:19 main.c-rw-r--r--1 root root 123 September 16:40 say_hello.c-rw-r--r --1 root root 20 September 15:58 say_hello.h-rwxr-xr-x 1 root root 6286 September 19:31 say_hello.so-rw-r--r--1 root root 4 16 September 16:39 time_print.c-rw-r--r--1 root root 69 September 15:00 time_print.h[[email protected] include_test]# [[ Email protected] include_test]# g++-o time_print.so-fpic-shared-i.-L TIME_PRINT.C say_hello.so[[email  Protected] include_test]# ll total dosage 36-rw-r--r--1 root root 149 September 16:19 main.c-rw-r--r--1 root root 123 September 14 16:40 say_hello.c-rw-r--r--1 root root 20 September 15:58 say_hello.h-rwxr-xr-x 1 root root 6286 September 19:31 say_hello.so-rw-r-  -r--1 root root 416 September 16:39 time_print.c-rw-r--r--1 root root 69 September 15:00 time_print.h-rwxr-xr-x 1 root root 7117 September 19:31 time_print.so[[Email protected] include_test]# 
If the-rpath-link option is not specified, the link fails
[Email protected] include_test]# g++-o main main.c time_print.so-i.-l./usr/bin/ld:warning:say_hello.so, needed by Ti  Me_print.so, not found (try Using-rpath or-rpath-link) time_print.so:undefined reference to ' Say_hello () ' Collect2:ld return 1[[email protected] include_test]#
If you specify the-rpath-link option, you can successfully link
[Email protected] include_test]# g++-o main main.c time_print.so-i.-L.-wl,-rpath-link,. [[email protected] include_test]# ll total dosage 44-rwxr-xr-x 1 root root 6999 September  19:37 main-rw-r--r--1 root root  149 9 Month  16:19 main.c-rw-r--r--1 root root  123 September  16:40 say_hello.c-rw-r--r--1 root root   20 September  14 15: Say_hello.h-rwxr-xr-x 1 root root 6286 September  19:31 say_hello.so-rw-r--r--1 root root  416 September  16:39 time_ print.c-rw-r--r--1 root root   69 September  15:00 time_print.h-rwxr-xr-x 1 root root 7117 September  19:31 time_print . So[[email Protected] include_test]#
View shared library dependencies
[[email protected] include_test]# ldd say_hello.so linux-vdso.so.1 = (0x00007fffb53ff000) libstdc        ++.so.6 =/usr/lib64/libstdc++.so.6 (0x00007f56915d4000) libm.so.6 =/lib64/libm.so.6 (0x00007f5691350000) Libgcc_s.so.1 =/lib64/libgcc_s.so.1 (0x00007f5691139000) libc.so.6 =/lib64/libc.so.6 (0x00007f5690 da5000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) [[email protected] include_test]# [[Email protec Ted] include_test]# ldd time_print.so linux-vdso.so.1 = (0x00007fff50cfa000) say_hello.so = not fo und libstdc++.so.6 =/usr/lib64/libstdc++.so.6 (0x00007ff0f7829000) libm.so.6 =/lib64/libm.so.6 (0x 00007ff0f75a5000) libgcc_s.so.1 =/lib64/libgcc_s.so.1 (0x00007ff0f738f000) libc.so.6 =/lib64/libc. So.6 (0x00007ff0f6ffa000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) [[email protected] include_test]# [[ Email protected] Include_test]# ldd Main linux-vdso.so.1 = (0x00007fffc55ff000) time_print.so = not found LIBSTD         c++.so.6 =/usr/lib64/libstdc++.so.6 (0x0000003899800000) libm.so.6 =/lib64/libm.so.6 (0x000000388dc00000) Libgcc_s.so.1 =/lib64/libgcc_s.so.1 (0x0000003898000000) libc.so.6 =/lib64/libc.so.6 (0x000000388 c800000)/lib64/ld-linux-x86-64.so.2 (0x000000388c400000) [[email protected] include_test]#
Execute the program
[Email protected] include_test]#/main./main:error while loading gkfx libraries:time_print.so:cannot open shared OB ject file:no such file or directory[[email protected] include_test]# [[email protected] include_test]# ld_library_path=. ./maincurrent timestamp = Sep 19:41:00hello world! [Email protected] include_test]#
Finally, a discussion of the rpath recursion problem is given: "resursive linking with rpath"



Shared library nesting dependency problem under "original" Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.