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

标签 macos cmake mach-o

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

在 Linux 上,它只是类似于 VERSION 5.4.2SOVERSION 5表示该库与 5.0 及更新版本兼容,以及 VERSION<major>.<minor> 形式用作 DLL 镜像版本在 Windows 上(我不确定 SOVERSION 在 Windows 上有什么区别)。

但是,the docs for the referenced FRAMEWORK target property 中的示例说明您可能如何拥有 VERSION 16.4.0SOVERSION 1.0.0在 Mach-O 平台上(我对构建框架不感兴趣,只是想知道国外的版本控制方案。)

版本控制在 Mach-O 世界中是如何工作的?如果我删除某些功能,我习惯于只是撞主要版本,这将是兼容性中断,那么版本 16.4.0 的库怎么可能与该库的 1.0.0 版本保持兼容? “兼容”是什么意思?

最佳答案

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

现在,让我们从 mach-o/loader.h 的摘录开始 header (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 */
};

正如评论中所解释的,struct dylib嵌入在库中以及针对它的二进制链接中,两者都包含 current_version 的副本。和 compatibility_version .后者的工作原理在那里有解释,但前者不是地址。
相关文档可以在 dyld 上找到。手册页(来源是 here ,但在 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.

简而言之:
  • compatibility_version用于确定库对于想要加载它的二进制文件是否“足够新”。
  • current_version用于在有多个可用库时选择一个库。

  • 至于您对当前版本 16.4.0 的困惑兼容版本为 1.0.0 : 从看 some sources ,每当引入任何类型的功能时,Apple 似乎都会提升主要版本,并且使用次要版本几乎只修复错误,AFAIK。
    所以他们叫什么16.4.0 ,我可能会调用 1.16.4 . ;)

    关于macos - Mach-O 平台上的库版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51940963/

    相关文章:

    c++ - cmake 将子目录链接在一起

    windows - 在 cmake 中强制构建 x64 位

    ios - GoogleCast.framework 出现重复符号错误

    macos - Mac OS X 检查是否安装了闪存

    CSS:如何增加 OSX 提交按钮的大小

    c - 我应该修改什么以仅预处理整个项目中的所有 C 文件?

    ios - 使用 drawGlossAndGradient() 的 Mach-O 错误

    ios - Apple Mach-O 链接

    ruby - 安装 Ruby gems 不适用于 Home Brew

    macos - 如何使用 bash 检查文件路径是否安装在 OS X 中?