Q: I would like to distribute my package as a library that can be
linked into programs, but I'm unsure how to go about it....
A: First, you need to compile all your sources into object
files, like this:
gcc -c -Wall -O2 file1.c gcc -c -Wall -O2 file2.c gcc -c -Wall -O2 file3.c ...
The only GCC switch in this example that's required is
rest are just recommended for better code generation and diagnostics.
Once you have the object files ready, use the
utility to create a library, let's call it
libacme.a, like this:
ar rvs libacme.a file1.o file2.o file3.o ...
rvs flags tell
ar to put named files into the
library, replacing any previous versions of these files if necessary,
print the names of object files as it puts them into the library, and
add an object-file index to the library, which makes it link faster.
If you use RHIDE, you can create a library by specifying a file
.a extension as the main target in the project (choose
Project | Main Target Name and enter a file name such as
The library is now ready to use. The simplest way to force the compiler to use it while linking is to mention its name in the link command line, like this:
gcc -o myprog.exe myprog.c libacme.a
This is better than just listing in the command line all the object files in the library, since the latter will cause the linker to link in all the object files, even those which aren't used by the program.
The name of the library which begins with a
lib and ends with a
.a extension is a convention used for convenience. When the link
command line includes an argument
-lXXYYZZ, GCC (and all Unix
compilers) will look for a file
libXXYYZZ.a in every directory
they search by default. So, if your library
installed in the DJGPP
lib subdirectory, the user can instruct
GCC to look into it by appending
-lacme to the link command line.
Other systems might be configured to look for different names when a
switch such as
-lfoo is mentioned. For example, Linux might look
/usr/lib for files
libfoo.so.*, while Alpha/VMS will
SYS$GNU:[LIBRARIES]FOO.LIB;*. Windows 98, of course,
will look for something monstrously long like
If you don't follow this convention, you will need to type the full name
of the library file.
If you need to update a certain object file in a library, use the same command ar rvs library-name object-name as above, but only with the name(s) of the object file(s) you need to replace.
ar is documented in the Binutils docs. To read, type this from
the DOS prompt:
info binutils ar