What is the difference between an object file and a static library (archive file)?

It seems the archive file can be generated from the object file:

ar rvs libprofile.a profile.o 

What is the difference between an object file and an archive?

It seems to me that both can be used directly with gcc, for example:

gcc *.c profile.o or gcc *.c libprofile.a

What's the difference?

+21
object archive
May 30 '11 at 14:25
source share
1 answer

A static library is a collection of one or more object files with an index for quick search. There are some minor differences in how the compiler handles them. You refer to the object file as follows:

 gcc f1.o f2.o -o myexe 

with libraries you can also do this:

 gcc f1.o libf2.a -o myexe 

or you can use the shortened version:

 gcc d1.o -lf2 -L. -o myexe 

In addition, gcc will ALWAYS link .o files, but it will only look for libraries and reference them if there are still undefined names.

+12
May 30 '11 at 14:27
source share



All Articles