It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
如何对使用GCC for Linux创建的共享对象文件进行版本控制

请用几个例子解释一下

最佳答案

LD_LIBRARY_PATH中可以存在共享库的多个版本。

例如:

/usr/lib/libform.so -> libform.so.5
/usr/lib/libform.so.5 -> libform.so.5.9
/usr/lib/libform.so.5.9
/usr/lib/libform.so.6 -> libform.so.6.0
/usr/lib/libform.so.6.0


上面的示例中存在符号链接,因为在链接时,如果您只提到-lform,它会根据符号链接自动为您选择正确的库。

当二进制文件与一组共享库链接时,它将请求该库的特定版本。可以使用ldd确定二进制文件所依赖的库列表

$ ldd /usr/bin/python
    linux-vdso.so.1 (0x00007ffffa5fe000)
    libpython2.7.so.1.0 => /usr/lib64/libpython2.7.so.1.0 (0x00007ff6e9b6c000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff6e9950000)
    libc.so.6 => /lib64/libc.so.6 (0x00007ff6e95ab000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007ff6e93a7000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007ff6e91a4000)
    libm.so.6 => /lib64/libm.so.6 (0x00007ff6e8ead000)
    /lib64/ld-linux-x86-64.so.2 (0x00007ff6e9f0a000)


在上面的示例中,python依赖于libm.so.6,而不仅仅是libm.so

库文件名中的版本控制通常采用以下形式:

libSOMETHING.so.VERSION
libSOMETHING.so.MAJOR_VERSION.MINOR_VERSION


一些库有时还具有次要编号或补丁编号。

每个库都将一个名为soname的字符串嵌入该库中,编译时和运行时链接程序均会检查该版本的兼容性。

例如,由于主版本号的更改,针对libform.so.5编译的二进制文件将与libform.so.5.9libform.so.5.9.1一起使用,但不适用于libform.so.6

要将soname信息构建到库中,您需要执行以下操作:

gcc -fPIC -shared -Wl,-soname,libfoo.so.1  -o libfoo.so.1.0.0 foo.c bar.c baz.c


ld的手册页中:

-soname=name
 When creating an ELF shared object, set the internal DT_SONAME
 field to the specified name.  When an executable is linked with
 a shared object which has a DT_SONAME field, then when the
 executable is run the dynamic linker will attempt to load the
 shared object specified by the DT_SONAME field rather than the
 using the file name given to the linker.

关于c++ - 如何对使用GCC for Linux创建的共享对象文件进行版本控制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15264693/

10-14 18:13
查看更多