Mach-O 平台上的库版本控制

4 macos mach-o cmake

当目标属性的文档SOVERSION提到在 OS X 和 iOS 等 Mach-O 系统上,它对应于“兼容版本”,而VERSION对应于“当前版本”时,我正在考虑对一个小型个人项目的共享库进行版本控制”。

在 Linux 上,它只是类似 和 的东西,VERSION 5.4.2表明SOVERSION 5该库与 5.0 及更高版本兼容,并在 Windows 上VERSION用作 DLL 映像版本<major>.<minor>(我不确定SOVERSION在 Windows 上有什么区别)。

然而,文档中引用的FRAMEWORK目标属性的示例说明了您在 Mach-O 平台VERSION 16.4.0上的可能方式SOVERSION 1.0.0(我对构建框架不感兴趣,只是想知道外部版本控制方案。)

Mach-O 世界中版本控制是如何工作的?我习惯于在删除某些功能时直接升级主要版本,这将导致兼容性中断,那么版本 16.4.0 的库怎么可能仍然与库的 1.0.0 版本兼容呢?“兼容”是什么意思?

Sig*_*uza 7

首先,为了解决这个问题,框架只是命名的 dylib,Something.framework/Something而不是libsomething.dylib. 但文件格式完全相同,因此在这篇文章中我将简单地将它们称为 dylib。

mach-o/loader.h现在,让我们从标头(Mach-O 文件格式事实上的权威来源)中摘录开始:

/*
 * Dynamicly linked shared libraries are identified by two things.  The
 * pathname (the name of the library as found for execution), and the
 * compatibility version number.  The pathname must match and the compatibility
 * number in the user of the library must be greater than or equal to the
 * library being used.  The time stamp is used to record the time a library was
 * built and copied into user so it can be use to determined if the library used
 * at runtime is exactly the same as used to built the program.
 */
struct dylib {
    union lc_str  name;         /* library's path name */
    uint32_t timestamp;         /* library's build time stamp */
    uint32_t current_version;       /* library's current version number */
    uint32_t compatibility_version; /* library's compatibility vers number*/
};

/*
 * A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
 * contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
 * An object that uses a dynamically linked shared library also contains a
 * dylib_command (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
 * LC_REEXPORT_DYLIB) for each library it uses.
 */
struct dylib_command {
    uint32_t    cmd;        /* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
                       LC_REEXPORT_DYLIB */
    uint32_t    cmdsize;    /* includes pathname string */
    struct dylib    dylib;      /* the library identification */
};
Run Code Online (Sandbox Code Playgroud)

正如评论中所解释的, astruct dylib嵌入在库以及针对它的二进制链接中,两者都包含current_version和的副本compatibility_version。那里解释了后者如何工作,但前者不是地址。
相关文档可以在手册页上找到dyld(源代码在这里,但在外部看起来不太漂亮man):

DYLD_VERSIONED_FRAMEWORK_PATH
    This  is a colon separated list of directories that contain potential override frame-
    works.  The dynamic linker searches  these  directories  for  frameworks.   For  each
    framework  found  dyld  looks  at  its  LC_ID_DYLIB  and gets the current_version and
    install name.  Dyld then looks for the framework at the install name path.  Whichever
    has the larger current_version value will be used in the process whenever a framework
    with that install name is required.  This is similar  to  DYLD_FRAMEWORK_PATH  except
    instead  of  always overriding, it only overrides is the supplied framework is newer.
    Note: dyld does not check the framework's Info.plist to find its version.  Dyld  only
    checks the -current_version number supplied when the framework was created.

[...]

DYLD_VERSIONED_LIBRARY_PATH
    This is a colon  separated  list  of  directories  that  contain  potential  override
    libraries.  The dynamic linker searches these directories for dynamic libraries.  For
    each library found dyld looks at its LC_ID_DYLIB and  gets  the  current_version  and
    install  name.   Dyld then looks for the library at the install name path.  Whichever
    has the larger current_version value will be used in the  process  whenever  a  dylib
    with  that  install  name  is  required.  This is similar to DYLD_LIBRARY_PATH except
    instead of always overriding, it only overrides is the supplied library is newer.
Run Code Online (Sandbox Code Playgroud)

简而言之:

  • compatibility_version用于确定库对于想要加载它的二进制文件来说是否“足够新”。
  • current_version当有多个库可用时,用于选择一个库。

至于您对当前版本16.4.0与兼容版本的困惑:从一些来源1.0.0来看,每当引入任何类型的功能时,Apple 似乎都会升级主要版本,并且几乎只使用次要版本错误修复,据我所知。 所以他们怎么称呼,我可能就会怎么称呼。;)
16.4.01.16.4