Shared library mysteriously doesn't get linked to application

人盡茶涼 提交于 2019-12-08 01:35:58

问题


I have a shared library (libhoard.so) that I'm trying to link with a simple test binary. However, depending on the machine I compile on the shared library doesn't show up in the test binary. I'm not sure what differences exist on the machines and is partly why I'm asking the question. I'm curious what I can do to troubleshoot why the shared library doesn't show up on the test binary on the "broken" machine?


I used this command to compile both binaries (libhoard.so is in the same directory):

$ g++ -L. -lhoard hoard_test.o 

Broken machine:

$ ldd a.out 
  linux-gate.so.1 =>  (0x00858000)
  libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0x004dc000)
  libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00aaf000)
  libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0x00675000)
  /lib/ld-linux.so.2 (0x00d18000)
  libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0x0040d000)

Working machine:

$ ldd a.out 
  linux-gate.so.1 =>  (0x00110000)
  libhoard.so (0x00111000) <----------------- THERE IT IS!
  libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x03ba8000)
  libm.so.6 => /lib/libm.so.6 (0x007a9000)
  libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00bf7000)
  libc.so.6 => /lib/libc.so.6 (0x0063e000)
  libdl.so.2 => /lib/libdl.so.2 (0x007d4000)
  libpthread.so.0 => /lib/libpthread.so.0 (0x007db000)
  /lib/ld-linux.so.2 (0x0061e000)

Here is some random version information:

Broken machine:

$ uname -srv
Linux 2.6.38-11-generic #50-Ubuntu SMP Mon Sep 12 21:18:14 UTC 2011
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

Working machine:

$ uname -srv
Linux 2.6.25.3-18.fc9.i686 #1 SMP Tue May 13 05:38:53 EDT 2008
$ g++ --version
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)

回答1:


tl;dr version: Add -Wl,--no-as-needed to the link command.

After a series of experimentation and conversations with the OP, I've figured out what's going on.

In the latest version of Ubuntu, ld uses --as-needed by default. What that does is to remove references to libraries that are not explicitly required.

The way Hoard works is as an LD_PRELOAD library. i.e., you are not supposed to need to use functions in libhoard.so directly. Of course, you could link in libhoard directly if you wanted to...unless --as-needed is used, of course.

After discovering this, the solution is simple. Just add -Wl,--no-as-needed to the gcc linking command.



来源:https://stackoverflow.com/questions/8814707/shared-library-mysteriously-doesnt-get-linked-to-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!