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

泄露秘密 提交于 2019-12-03 10:52:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6177498/whats-the-difference-between-object-file-and-static-libraryarchive-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!