什么是在OSX的.so和名为.dylib之间的区别是什么? [英] What are the differences between .so and .dylib on osx?

查看:2791
本文介绍了什么是在OSX的.so和名为.dylib之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

名为.dylib是在OSX动态库扩展,但它从来都不是很清楚,我的时候,我不能/不应该使用传统的UNIX的.so共享对象。

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

有些我有问题:


  • 在概念层面,是什么。所以之间的主要差异和名为.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 被苹果推荐,但多数移植的软件使用的.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.

这两个dylibs和软件包可以使用 DL 的API(如的dlopen 动态加载, dlclose )。这是不可能的,就好像共享库对束进行链接。但是,它可能是一个束对真正的共享库链接;在束加载的时候也会被自动加载。

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,有没有办法动态加载库。一组dyld的的API(如 NSCreateObjectFileImageFromFile NSLinkModule )与10.1介绍装载和卸载捆绑,但他们没'吨dylibs工作。与10.3加入捆绑合作A 的dlopen 兼容库;在10.4,的dlopen 被改写为dyld的土生土长的一部分,并增加了对加载(但不卸载)dylibs支持。最后,10.5增加了支持使用 dlclose 与dylibs和德precated 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系统,都使用相同的文件格式;任何一块共享code可以用来作为库和动态加载。

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中,<一个href=\"https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Bundle.html\">\"bundle\"可以的的指向目录与持有可执行code和由code使用的资源的标准化结构。有一些概念重叠(尤其是与可加载的束喜欢的插件,其一般含有一个Mach-O的束的形式的可执行code)中,但它们不应该与上面所讨论的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 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

这篇关于什么是在OSX的.so和名为.dylib之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆