26

Seems archive file can be generated from object file:

ar rvs libprofile.a profile.o

What's the difference between object file and archive file?

It seems to me that both can be used with gcc directly,e.g.:

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

What's the difference?

compile-fan
  • 15,145
  • 19
  • 53
  • 70
  • 1
    possible duplicate of [Why create a .a file from .o for static linking?](http://stackoverflow.com/questions/1852941/why-create-a-a-file-from-o-for-static-linking) – Vilhelm Gray Mar 20 '14 at 15:13

1 Answers1

18

The static library is a collection of one or more object files, with an index to allow rapid searching. There are some minor differences in how the compiler deals with them. With an object file you link like this:

gcc f1.o f2.o -o myexe

with libraries you can also do that:

gcc f1.o libf2.a -o myexe

or you can use shorthand:

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

Also, gcc will ALWAYS link .o files, but it will only search libraries and link from them if there are undefined names still to resolve.