macOS上的.so和.dylib有什么区别?

编程入门 行业动态 更新时间:2024-10-27 18:17:59
本文介绍了macOS上的.so和.dylib有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

.dylib是macOS上的动态库扩展,但是当我不能/不应该使用传统的unix .so共享库时,这对我来说还不清楚.

.dylib is the dynamic library extension on macOS, but it's never been clear to me when I can't / shouldn't use a traditional unix .so shared object.

我有一些问题:

  • 从概念上讲,.so和.dylib之间的主要区别是什么?
  • 何时/应该何时使用另一个?
  • 编译技巧和技巧(例如,替换gcc -shared -fPIC,因为它在osx上不起作用)
推荐答案

Mac OS X用于可执行文件和库的Mach-O目标文件格式区分了共享库和动态加载模块.使用 otool -hv some_file 查看 some_file 的文件类型.

The Mach-O object file format used by Mac OS X for executables and libraries distinguishes between shared libraries and dynamically loaded modules. Use otool -hv some_file to see the filetype of some_file.

Mach-O共享库的文件类型为 MH_DYLIB ,并带有扩展名.dylib.它们可以与通常的静态链接器标志链接,例如 -lfoo 用于libfoo.dylib.可以通过将 -dynamiclib 标志传递给编译器来创建它们.( -fPIC 是默认设置,无需指定.)

Mach-O shared libraries have the file type MH_DYLIB and carry the extension .dylib. They can be linked against with the usual static linker flags, e.g. -lfoo for libfoo.dylib. They can be created by passing the -dynamiclib flag to the compiler. (-fPIC is the default and needn't be specified.)

可加载模块在Mach-O语言中称为捆绑".它们的文件类型为 MH_BUNDLE .他们可以携带任何扩展名;Apple建议使用扩展名 .bundle ,但是出于兼容性考虑,大多数移植的软件都使用 .so .通常,您将为捆绑包使用插件来扩展应用程序;在这种情况下,捆绑软件将链接到应用程序二进制文件,以访问应用程序的导出API.可以通过将 -bundle 标志传递给编译器来创建它们.

Loadable modules are called "bundles" in Mach-O speak. They have the file type MH_BUNDLE. They can carry any extension; the extension .bundle is recommended by Apple, but most ported software uses .so for the sake of compatibility. Typically, you'll use bundles for plug-ins that extend an application; in such situations, the bundle will link against the application binary to gain access to the application’s exported API. They can be created by passing the -bundle flag to the compiler.

可以使用 dl API(例如 dlopen , dlclose )动态加载dylib和bundle.无法像捆绑包一样共享捆绑包.但是,捆绑包可能会链接到实际的共享库.捆绑包加载后,这些文件将自动加载.

Both dylibs and bundles can be dynamically loaded using the dl APIs (e.g. dlopen, dlclose). It is not possible to link against bundles as if they were shared libraries. However, it is possible that a bundle is linked against real shared libraries; those will be loaded automatically when the bundle is loaded.

从历史上看,差异更大.在Mac OS X 10.0中,无法动态加载库.10.1中引入了一组Dyld API(例如 NSCreateObjectFileImageFromFile , NSLinkModule )来加载和卸载捆绑软件,但它们不适用于dylib.在10.3中添加了一个与bundles兼容的 dlopen 兼容性库.在10.4中, dlopen 被重写为dyld的本机部分,并增加了对加载(但不卸载)dylib的支持.最后,10.5添加了对将 dlclose 与dylibs一起使用的支持,并弃用了dyld API.

Historically, the differences were more significant. In Mac OS X 10.0, there was no way to dynamically load libraries. A set of dyld APIs (e.g. NSCreateObjectFileImageFromFile, NSLinkModule) were introduced with 10.1 to load and unload bundles, but they didn't work for dylibs. A dlopen compatibility library that worked with bundles was added in 10.3; in 10.4, dlopen was rewritten to be a native part of dyld and added support for loading (but not unloading) dylibs. Finally, 10.5 added support for using dlclose with dylibs and deprecated the dyld APIs.

在Linux等ELF系统上,两者都使用相同的文件格式;任何共享代码段都可以用作库并进行动态加载.

On ELF systems like Linux, both use the same file format; any piece of shared code can be used as a library and for dynamic loading.

最后,请注意,在Mac OS X中,捆绑包" 也可以 引用具有标准化结构的目录,该目录包含可执行代码和该代码使用的资源.在概念上存在一些重叠(特别是与可加载包"(如插件,通常包含Mach-O包形式的可执行代码)重叠),但不应将它们与上述Mach-O包混淆.

Finally, be aware that in Mac OS X, "bundle" can also refer to directories with a standardized structure that holds executable code and the resources used by that code. There is some conceptual overlap (particularly with "loadable bundles" like plugins, which generally contain executable code in the form of a Mach-O bundle), but they shouldn't be confused with Mach-O bundles discussed above.

其他参考:

  • Fink移植指南,此答案的基础(尽管已经过时了,因为它是为Mac OS X 10.3编写的.)
  • ld(1)和 dlopen(3)
  • 动态库编程主题
  • Mach-O编程主题
  • Fink Porting Guide, the basis for this answer (though pretty out of date, as it was written for Mac OS X 10.3).
  • ld(1) and dlopen(3)
  • Dynamic Library Programming Topics
  • Mach-O Programming Topics

更多推荐

macOS上的.so和.dylib有什么区别?

本文发布于:2023-10-08 02:22:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   macOS   dylib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!