Search code examples
objectarchive

What's the difference between object file and static library(archive file)?


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?


Solution

  • 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.