How do I create a library of object files?

First, you need to compile all your sources into object .o 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 -c, the rest are just recommended for better code generation and diagnostics.

Once you have the object files ready, use the ar ("Archiver") utility to create a library, let's call it libacme.a, like this:

 ar rvs libacme.a file1.o file2.o file3.o ...

The 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 with a .a extension as the main target in the project (choose Project | Main Target Name and enter a file name such as libacme.a).

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 libacme.a is 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 in /usr/lib for files libfoo.so.*, while Alpha/VMS will look for SYS$GNU:[LIBRARIES]FOO.LIB;*. Windows 98, of course, will look for something monstrously long like C:\Windows\Program Files\Vendors\GNU\gcc\libraries\foo.lib. 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

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!