macOS 上的 .so 和 .dylib 有什么区别? [英] What are the differences between .so and .dylib on macOS?
问题描述
.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
.他们可以携带任何分机;扩展名 .bundle
是 Apple 推荐的,但大多数移植的软件为了兼容性而使用 .so
.通常,您将为扩展应用程序的插件 使用捆绑包;在这种情况下,bundle 将链接到应用程序二进制文件以访问应用程序的导出 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.
dylib 和包都可以使用 dl
API(例如 dlopen
、dlclose
)动态加载.不可能像捆绑包一样链接到共享库.但是,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 中添加了一个用于捆绑包的 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 中,"bundle" 可以也指代具有标准化结构的目录,其中包含可执行代码和该代码使用的资源.有一些概念上的重叠(特别是像插件这样的可加载包",它们通常包含 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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!