quarta-feira, 15 de outubro de 2008

Program Library HOWTO

Program Library HOWTO

Program Library HOWTO

This is the main web site for the Program Library HOWTO. This HOWTO for programmers discusses how to create and use program libraries on Linux using the GNU toolset. A ``program library'' is simply a file containing compiled code (and data) that is to be incorporated later into a program; program libraries allow programs to be more modular, faster to recompile, and easier to update. Program libraries can be divided into three types: static libraries, shared libraries, and dynamically loaded (DL) libraries.

This document is part of the Linux Documentation Project (LDP), and distributed in various Linux distributions. However, note that the LDP's version or the version in a CD-ROM distribution may not be as current as the main (master) web site at "http://www.dwheeler.com/program-library".

Downloading/Reading

You can see the HOWTO in HTML (good for on-line browsing), PDF, RTF, Postscript, ASCII Text (not recommended), or SGML (DocBook DTD) formats. You can also see the ChangeLog, and users of the SGML format may find the Makefile useful. The document includes a lot of trivial examples; you can download the examples as a tarball. Or, to try out various library approaches, you can directly view demo_dynamic.c, demo_use.c, libhello.c, and libhello.h.

If you have comments, proposed improvements, or intend to translate it to another human language, please contact me.

Other files are available here too. If you don't believe that /lib is searched before /usr/lib, you can run the test script originally developed by Michael Kerrisk at usrlib-test, which requires source files mod1.c and sltest.c. You can also view various ELF-related specifications; see elf-linux.ps and elfspec.ps.

A Personal Comment about Linux' program library design

As far as I can tell, anyone who starts learning about Linux's approach to library management goes through three stages:

  1. This is kind of odd, maybe even crufty. Multiple symbolic links? Environment variable overrides?
  2. This is really odd, why do they DO it this way?
  3. It saved my bacon again! This is amazing! I love it!

Linux is the inheritor of a lot of smart thinking about library management on a system. Some systems have what's called "DLL Hell", where incompatible libraries stomp all over each other; that can make it impossible or very difficult to run some different programs simultaneously. That doesn't happen on a Linux system; its mechanisms are based on over 35 years of understanding how things can go wrong, and how to prevent them where possible, or manage them when they do. For many years, Linux systems have been able to manage many different versions of components without trouble.

Historically Linux systems have had a different problem: there's so much software, and so many components reuse other components, that it's sometimes hard to make sure that you download all the right versions of the components to install something. Today, tools like apt and yum (and portage before them) have made that problem pretty much disappear, by handling installation downloads automatically.

Translations

Translations I know of are listed here:

There may be other translations available; see the Linux Documentation Project.

I hope to mention or link to other translations as I learn about them. Please contact me before translating, so that duplicate work can be avoided (for example, perhaps multiple translators could divide the work). If one becomes available, I cannot guarantee that the translations accurately reflect the original English work; I'm sorry, but I'm simply not qualified to judge that.

Feel free to see my main web site at http://www.dwheeler.com.




Program Library HOWTO

Abstract

This HOWTO for programmers discusses how to create and use program libraries on Linux. This includes static libraries, shared libraries, and dynamically loaded libraries.


Introduction

This HOWTO for programmers discusses how to create and use program libraries on Linux using the GNU toolset. A ``program library'' is simply a file containing compiled code (and data) that is to be incorporated later into a program; program libraries allow programs to be more modular, faster to recompile, and easier to update. Program libraries can be divided into three types: static libraries, shared libraries, and dynamically loaded (DL) libraries.

This paper first discusses static libraries, which are installed into a program executable before the program can be run. It then discusses shared libraries, which are loaded at program start-up and shared between programs. Finally, it discusses dynamically loaded (DL) libraries, which can be loaded and used at any time while a program is running. DL libraries aren't really a different kind of library format (both static and shared libraries can be used as DL libraries); instead, the difference is in how DL libraries are used by programmers. The HOWTO wraps up with a section with more examples and a section with references to other sources of information.

Most developers who are developing libraries should create shared libraries, since these allow users to update their libraries separately from the applications that use the libraries. Dynamically loaded (DL) libraries are useful, but they require a little more work to use and many programs don't need the flexibility they offer. Conversely, static libraries make upgrading libraries far more troublesome, so for general-purpose use they're hard to recommend. Still, each have their advantages, and the advantages of each type are described in the section discussing that type. Developers using C++ and dynamically loaded (DL) libraries should also consult the ``C++ dlopen mini-HOWTO''.

It's worth noting that some people use the term dynamically linked libraries (DLLs) to refer to shared libraries, some use the term DLL to mean any library that is used as a DL library, and some use the term DLL to mean a library meeting either condition. No matter which meaning you pick, this HOWTO covers DLLs on Linux.

This HOWTO discusses only the Executable and Linking Format (ELF) format for executables and libraries, the format used by nearly all Linux distributions today. The GNU gcc toolset can actually handle library formats other than ELF; in particular, most Linux distributions can still use the obsolete a.out format. However, these formats are outside the scope of this paper.

If you're building an application that should port to many systems, you might consider using GNU libtool to build and install libraries instead of using the Linux tools directly. GNU libtool is a generic library support script that hides the complexity of using shared libraries (e.g., creating and installing them) behind a consistent, portable interface. On Linux, GNU libtool is built on top of the tools and conventions described in this HOWTO. For a portable interface to dynamically loaded libraries, you can use various portability wrappers. GNU libtool includes such a wrapper, called ``libltdl''. Alternatively, you could use the glib library (not to be confused with glibc) with its portable support for Dynamic Loading of Modules. You can learn more about glib at http://developer.gnome.org/doc/API/glib/glib-dynamic-loading-of-modules.html. Again, on Linux this functionality is implemented using the constructs described in this HOWTO. If you're actually developing or debugging the code on Linux, you'll probably still want the information in this HOWTO.

This HOWTO's master location is http://www.dwheeler.com/program-library, and it has been contributed to the Linux Documentation Project (http://www.linuxdoc.org). It is Copyright (C) 2000 David A. Wheeler and is licensed through the General Public License (GPL); see the last section for more information.

Nenhum comentário: