简体   繁体   English

在C ++项目中使用共享库的问题

[英]Issue with using shared libraries in C++ projects

I am trying to change MongoDB's source and use functions from an external library. 我正在尝试更改MongoDB的源并使用外部库中的函数。 I have been able to change the build scripts ( SCons files) and force the compiler to do extra linking with the libraries I am going to use. 我已经能够更改构建脚本( SCons文件),并强制编译器与要使用的库进行额外的链接。 Although I am not facing any issues while compiling the source code and linking the object files, I face an odd error when starting mongod . 尽管在编译源代码和链接目标文件时没有遇到任何问题,但是在启动mongod时遇到了奇怪的错误。 Here is the message that I am receiving (immediately before the mongod process terminates): 这是我收到的消息(在mongod进程终止之前):

./mongod: error while loading shared libraries: .1: cannot open shared object file: No such file or directory

I was able to fix this by moving the .so file of the library to /lib/x86_64-linux-gnu and rename it to .1, which is the path mongod expects to find the shared library. 我可以通过将库的.so文件移动到/ lib / x86_64-linux-gnu并将其重命名为.1来解决此问题,这是mongod期望找到共享库的路径。 I don't know what is the cause of this problem, but everything is fine with other libraries such as pthread . 我不知道是什么原因导致此问题,但是其他库(例如pthread)一切都很好。 Is there any specific solution for this problem? 有没有针对此问题的特定解决方案? In other words, what should I do to make the executable program look for the library file in other locations using proper naming (not .1)? 换句话说,我应该怎么做才能使可执行程序使用正确的命名(不是.1)在其他位置查找库文件?

Here is the output while LD_DEBUG=all mongod: 这是LD_DEBUG = all mongod时的输出:

  4564: 
  4564: file=libpthread.so.0 [0];  needed by ./mongod [0]
  4564: find library=libpthread.so.0 [0]; searching
  4564:  search cache=/etc/ld.so.cache
  4564:   trying file=/lib/x86_64-linux-gnu/libpthread.so.0
  4564: 
  4564: file=libpthread.so.0 [0];  generating link map
  4564:   dynamic: 0x00007f6ddccb7d50  base: 0x00007f6ddcaa0000   size: 0x000000000021c448
  4564:     entry: 0x00007f6ddcaa6cf0  phdr: 0x00007f6ddcaa0040  phnum:                  9
  4564: 
  4564: 
  4564: file=.1 [0];  needed by ./mongod [0]
  4564: find library=.1 [0]; searching
  4564:  search cache=/etc/ld.so.cache
  4564:   trying file=/lib/x86_64-linux-gnu/.1
  4564:  search path=/lib/x86_64-linux-gnu/tls/x86_64:/lib/x86_64-linux-gnu/tls:/lib/x86_64-linux-gnu/x86_64:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls/x86_64:/usr/lib/x86_64-linux-gnu/tls:/usr/lib/x86_64-linux-gnu/x86_64:/usr/lib/x86_64-linux-gnu:/lib/tls/x86_64:/lib/tls:/lib/x86_64:/lib:/usr/lib/tls/x86_64:/usr/lib/tls:/usr/lib/x86_64:/usr/lib        (system search path)
  4564:   trying file=/lib/x86_64-linux-gnu/tls/x86_64/.1
  4564:   trying file=/lib/x86_64-linux-gnu/tls/.1
  4564:   trying file=/lib/x86_64-linux-gnu/x86_64/.1
  4564:   trying file=/lib/x86_64-linux-gnu/.1
  4564:   trying file=/usr/lib/x86_64-linux-gnu/tls/x86_64/.1
  4564:   trying file=/usr/lib/x86_64-linux-gnu/tls/.1
  4564:   trying file=/usr/lib/x86_64-linux-gnu/x86_64/.1
  4564:   trying file=/usr/lib/x86_64-linux-gnu/.1
  4564:   trying file=/lib/tls/x86_64/.1
  4564:   trying file=/lib/tls/.1
  4564:   trying file=/lib/x86_64/.1
  4564:   trying file=/lib/.1
  4564:   trying file=/usr/lib/tls/x86_64/.1
  4564:   trying file=/usr/lib/tls/.1
  4564:   trying file=/usr/lib/x86_64/.1
  4564:   trying file=/usr/lib/.1
  4564: 
  ./mongod: error while loading shared libraries: .1: cannot open shared object file: No such file or directory

I somehow found the solution for my problem. 我以某种方式找到了解决我问题的方法。 Thought it might be also helpful to others. 认为这可能对其他人也有帮助。

Basically, when you are using make to create shared libraries, you should define the .so file name. 基本上,使用make创建共享库时,应定义.so文件名。 This was my case and I just set it to a proper value and everything is now working, at least I have fixed this error by doing so. 这是我的情况,我只是将其设置为适当的值,并且现在一切正常,至少我已通过这样做解决了该错误。

Looking it up in Google, I found that -soname option of gcc is designed to do so. 在Google中查找时,我发现gcc的 -soname选项旨在实现此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM