|
What is a library? What are they for? What is a library? What are they for?In computer programming jargon, a library is a repository for object code, organised in a way that permits re-use of that object code (whether or not you have the original source code). Once the object code is compiled by a particular implementation, it can be stored in the library, which can then be linked into as many programs as you like. When you link the library to the program, any object code required by the program is copied from the library into the program. The benefits of using libraries are:
This last-named benefit may seem to Open Source developers to be more of a disadvantage, of course, but that's a socio-economic issue, not a technical issue! How do I create a library?The precise mechanism for creating a library varies depending on your library-building tool. In this tutorial (which is at a very basic level, and is intended to get you up and running, rather than to make you an expert), I'll show you how to do this using each of three popular modern library-building tools on two different operating systems (well, sort of three - but OS X is mostly like Linux in this regard - I'll point out where they differ, though). But first, we'll need some code to put into our library. To make it very clear that there's nothing special about library code, I'm going to use the simplest possible example code I can think of:
This code implements the
That's all the caller needs to know. Now let's compile our source, and stick the resulting object file into a library. I'll demonstrate using command line tools for various implementations. If you would rather use a GUI, and can't find out how to drive your particular GUI, ask on Usenet (or just use the command line, which is generally much easier!).
First, compile Linux
Note the Borland
Microsoft
Next, let's create the library. Linux
The "c" means "create the library"; if the library already exists, omit this flag. The "r" means "insert the file into the archive, replacing an existing file if necessary". Note that I've named the library "libmycode.a". There's a reason it starts with "lib", which we'll come to in due course. OS X
According to information received, this is the same as for Linux, except that
you must call
suffices, apparently. Borland
The Microsoft
How do I use a library?To use the library, we'll need a program. Here's one:
Save this as Let's build the program: Linux
The quotes around the full-stop (period) are optional. The
Clearly, you can keep OS X
As for Linux, apparently, except that I'm told that Borland
Microsoft
In each case, the program is built correctly, and But we're not finished quite yet. The library only has one function in it. If I went to my local lending library and found that they had only one book in stock, I'd probably be a bit disappointed (unless it was a really good book!). Let's find out how to add new object files to existing libraries. Here's another function:
You may wish to knock up a Linux
Borland
To replace an existing object file in the library, by the way, use
Microsoft Apparently (and I haven't tested this yet) you can add an object file to an existing Microsoft library by letting the library be one of the inputs to the lib command, with the new object(s) being the other input(s):
How should I organise code within the library?Well, it's up to you. Be aware, however, that some linkers (very possibly including the one you use) aren't able to extract functions from libraries. They can only extract object files from libraries. Thus, if your program needs just one function from a bunch of five all in the same object file, you could well end up with a bigger binary than necessary. "Well, yes, all right," you say, "but why is that a problem? After all, I could just write one function in each object file." Absolutely right. You can do that, and that's fine. So, there's one way you might organise your code to make it linker-friendly; just put one function in each object file. (The downside is that you end up with a lot of source files, and this increases the cost (or hassle, if you prefer) of file management.)
In any case, there are times when functions are pretty well bound
to be used together in a well-written program. Examples that spring to
mind include It's entirely up to you, though.
Where should I keep my libraries and header files?Again, this is up to you. Here's a suggestion which you are entirely free to adopt, adapt, or ignore entirely: Whilst the library is under development, treat it as you treat any other project.
Once it's settled down sufficiently that modifications of its existing
functions are likely to be rare, copy it to a well-known location. On Linux,
the obvious place is That's it. Suggestions for improvements to this page are most welcome. Kudos to EwIck for sorting out how to add object files to existing libraries in Microsoft C. |