Book HomeMac OS X for Unix GeeksSearch this book

5.4. Library Versions

Library version numbering is one area where Mac OS X differs from other Unix variants. In particular, the dynamic linker dyld checks both major and minor version numbers. Also, the manner in which library names carry the version numbers is different. On ELF systems, shared libraries are named with an extension similar to the following:

libname.so.major_version_no.minor_version_no

Typically, a symbolic link is created in the library named libname.so, which points to the most current version of the library. For example, on an ELF system like Solaris, libMagick.so.5.0.44 is the name of an actual library. If this is the latest installed version of the library, you can find symbolic links that point to this library in the same directory. These symbolic links are typically created during the installation process.

In this example, both libMagick.so and libMagick.so.5 are symbolic links that point to libMagick.so.5.0.44. Older versions of the library may also be present, such as libMagick.so.5.0.42. However, although older versions of the library may be present, whenever a newer version is installed, the symbolic links are updated to point to the latest version. This works because when you create a shared library, you need to specify the name of the library to be used when the library is called by a program at runtime.

TIP: In general, you should keep older versions of libraries around, just in case an application depends on them. If you are certain there are no dependencies, you can safely remove an older version.

On Mac OS X, the libMagick library is named libMagick.5.0.44.dylib, and the symbolic links libMagick.dylib and libMagick.5.dylib point to it. Older versions, such as libMagick.5.0.42.dylib, may also be found in the same directory. One difference that is immediately apparent on Mac OS X systems is that the version numbers are placed between the library name and the .dylib extension rather than at the end of the filename, as on other Unix systems (e.g., libMagick.so.5.0.42).

Another difference on Darwin is that the absolute pathname is specified when the library is installed. Thus, ldconfig is not used in Darwin, since paths to linked dynamic shared libraries are included in the executables. On an ELF system, you typically use ldconfig or set the LD_LIBRARY_PATH variable. In Darwin, use DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH (see the dyld man page for more details).

You can link against a particular version of a library by including the appropriate option for cc, such as -lMagick.5.0.42. Minor version checking is another way that the Mach-O format differs from ELF. To illustrate this, let's revisit Example 5-4, earlier in this chapter.

Suppose that the library shown in Example 5-4 will continue to be improved over time; minor bugs will be fixed, minor expanded capabilities will be added, and, in time, major new features will be added. In each of these cases, there will be a need to rename the library to reflect the latest version. Assume that the last version of the library is named libanswer.1.2.5.dylib. The major version number is 1, the minor revision is 2, and the bug-fix (i.e., fully compatible) revision number is 5. Example 5-8 illustrates how to update this library to release libanswer.1.2.6.dylib, which is fully compatible with the release 1.2.5, but contains some bug fixes.

In the makefile shown earlier in Example 5-5, replace the following lines:

libanswer.dylib: answer.o
        $(LD) -dynamiclib -install_name libanswer.dylib \
        -o libanswer.dylib answer.o

with the code shown in Example 5-8.

Example 5-8. Versioning the answer library

libanswer.dylib: answer.o 
     $(LD) -dynamiclib -install_name libanswer.1.dylib \
            -compatibility_version 1.2 -current_version 1.2.6 \
              -o libanswer.1.2.6.dylib  $(OBJS) 
      rm -f libanswer.1.dylib   libanswer.1.2.dylib  libanswer.dylib
      ln -s libanswer.1.2.6.dylib libanswer.1.2.dylib
      ln -s libanswer.1.2.6.dylib libanswer.1.dylib
      ln -s libanswer.1.2.6.dylib libanswer.dylib

Symbolic links are established to point to the actual library: one link reflecting the major revision, one reflecting the minor revision, and one that simply reflects the name of the library.

The compatibility version number checks that the library used by an executable is compatible with the library that was linked in creating the executable. This is why the phrase compatibility version makes sense in this context.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.