tags:

views:

382

answers:

2

.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.

Some of the questions I have:

  • At a conceptual level, what are the main differences between .so and .dylib?
  • When can/should I use one over the other?
  • Compilation tricks & tips (For example, the replacement for gcc -shared -fPIC, since that doesn't work on osx)
+4  A: 

This page addresses some of your questions, though it's somewhat out of date.

One Mach-O feature that hits many people by surprise is the strict distinction between shared libraries and dynamically loadable modules. On ELF systems both are the same; any piece of shared code can be used as a library and for dynamic loading. Use otool -hv some_file to see the filetype of some_file.

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. However, they can not be loaded as a module. (Side note: Shared libraries can be loaded dynamically through an API. However, that API is different from the API for bundles and the semantics make it useless for an dlopen() emulation. Most notably, shared libraries can not be unloaded.) [This is no longer true—you can use dlopen() with both dylibs and bundles. However, dylibs still can't be unloaded.]

Loadable modules are called "bundles" in Mach-O speak. They have the file type MH_BUNDLE. Since no component involved cares about it, they can carry any extension. The extension .bundle is recommended by Apple, but most ported software uses .so for the sake of compatibility. Bundles can be dynamically loaded and unloaded via dyld APIs, and there is a wrapper that emulates dlopen() on top of that API. [dlopen is now the preferred API.] 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.

To compile a normal shared library on OS X, you should use -dynamiclib and the extension .dylib. -fPIC is the default.

Additional references:

Miles
+1  A: 

The file .so is not a UNIX file extension for shared library.

It just happens to be a common one.

Check this page: http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
See line 3b.

Basically .dylib is the mac file extension used to indicate a shared lib.

Martin York
+1, UNIX/Linux does not rely on file extension to denote file type.
Ninefingers
@ninefingers. Correct. But some of the tools will use default values unless something is very explicit. e.g. Compilers will use there platform specific shared libray extension when the -l<lib> flag is used (actual flag may very across compilers).
Martin York