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
.他们可以携带任何扩展名;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中,
- 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
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.
其他参考:
这篇关于macOS上的.so和.dylib有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!