简体   繁体   English

C ++共享库

[英]C++ shared libraries

I am trying to get my head around the way shared libraries work in the c++ unix environment. 我试图弄清楚共享库在c ++ UNIX环境中的工作方式。 I understand we only need header files and no shared libraries specification when compiling code. 我了解我们在编译代码时只需要头文件,而无需共享库规范。 But if I want to create an executable or shared library from my compiled files, do I need to specify shared library dependencies (those are dynamic)? 但是,如果要从编译的文件中创建可执行文件或共享库,是否需要指定共享库依赖项(这些依赖项是动态的)? And do the paths of shared libraries need to match the path at runtime loading? 共享库的路径是否需要在运行时加载时匹配路径?

I am using Linux 2.6.18-164.11.1.el5 #1 SMP x86_64 GNU/Linux 我正在使用Linux 2.6.18-164.11.1.el5#1 SMP x86_64 GNU / Linux

I am having a problem where my code is not able to pick up a library at runtime. 我遇到了一个问题,我的代码无法在运行时拾取库。 I have tried setting LD_LIBRARY_PATH and PATH. 我尝试设置LD_LIBRARY_PATH和PATH。 But at runtime when I run the executable, I get the following error: Error: librc.so: cannot open shared object file: No such file or directory 但是在运行该可执行文件时,在运行时出现以下错误:错误:librc.so:无法打开共享库文件:没有这样的文件或目录

Sam 山姆

The headers are only for the compile phase. 标头仅用于编译阶段。 At link time, you usually have to specify which shared libs you are going to link to. 在链接时,通常必须指定要链接到的共享库。 You might see -L options to set locations to where shared libraries reside, and/or -l to specify which libraries to link. 您可能会看到-L选项来设置共享库所在的位置,和/或-l指定要链接的库。 There is usually also a switch on the command line to alert the linker as to whether you are using thread-safe versions of the libs or the 'regular' ones, and another switch to specify dynamic linking. 通常,在命令行上还会有一个开关来提醒链接器有关您使用的是libs还是“常规”版本的线程安全版本,以及另一个用于指定动态链接的开关。

At run time, whether you are starting the program that uses the libs, or running ldd to find out what it needs, the OS has a system for locating .so files, and this can vary from one unix version to another. 在运行时,无论您是启动使用lib的程序,还是运行ldd来查找所需内容,操作系统都具有用于查找.so文件的系统,并且该版本可能因一个unix版本而异。 The LD_LIBRARY_PATH variable specifies where to look for .so files, but may not be the full story, depending on the exact unix version in question. LD_LIBRARY_PATH变量指定在哪里查找.so文件,但可能不是完整的故事,具体取决于所讨论的确切Unix版本。 Also, you probably don't want to fiddle around with modifying LD_LIBRARY_PATH except from a throw-away shell, since it has system wide effects. 另外,您可能不想在修改LD_LIBRARY_PATH之外随意摆弄,因为它会带来系统范围的影响,因此只能扔掉外壳。 A better option is to check it the 'missing' .so files are or are not on the existing path set by LD_LIBRARY_PATH, and if not, try putting copies of them somewhere on that path. 更好的选择是检查LD_LIBRARY_PATH设置的现有路径中是否缺少“ .so”文件,如果不是,请尝试将其副本放在该路径中的某个位置。

At run time, dynamic libraries are searched: 在运行时,将搜索动态库:

  • in a path recorded in the executable (under linux with -rpath at link time, under Solaris with -R, using $ORIGIN in a directory name allows to specify a directory relative to the directory containing the executable) 在可执行文件中记录的路径中(在链接时带有-rpath的Linux下,在带有-R的Solaris下,在目录名中使用$ ORIGIN可以指定相对于包含可执行文件的目录的目录)

  • in the LD_LIBRARY_PATH (or equivalent, there are sometimes 64/32 bits variant). 在LD_LIBRARY_PATH中(或等效名称,有时会有64/32位变量)。 If a path has been recorded in the executable, LD_LIBRARY_PATH may not searched (under Linux it is searched after the recorded path if the executable has been linked with the option --enable-new-dtags; I don't remember Solaris behavior for now) 如果已在可执行文件中记录了路径,则可能不会搜索LD_LIBRARY_PATH(在Linux下,如果可执行文件已与选项--enable-new-dtags链接,则会在记录的路径后进行搜索;我现在不记得Solaris的行为)

  • in a set of system dependant directories (Linux allows to specify them in /etc/ld.so.conf and has a cache, see ldconfig) 在一组与系统相关的目录中(Linux允许在/etc/ld.so.conf中指定它们并具有缓存,请参阅ldconfig)

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

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