What is the relationship of .lib and .obj to each other and my project in c++?
How do .lib and .obj files relate to each other? What 开发者_StackOverflowis their purpose? Is a .lib just a collection of .obj files? If so are the .obj's then stored inside the .lib making the .obj's unnecessary?
Typically, the .obj
files refer to object files. This is a source file in its compiled form. For example, a main.cpp
and foo.cpp
would produce main.obj
and foo.obj
.
It is then the linkers job to link them together, so that main.obj
can reach functions defined in foo.obj
and vice-versa. The linker will output your binary file, which is the .lib
(or .a
, or .exe
, or .dll``, etc).
So in a loose sense, yes, the binary output (.lib
in your case) is the collection of linked .obj
files. Once you are finished compiling, and want to use the library, you only need other programs to link with the .lib
. The .obj
are what's considered intermediate files, and are not needed after linking is completed.
This depends. If the .lib file is a static library, then it is more or less just a collection of .obj files. If you are making or using a DLL, then the .lib file is just an import library, with information about which symbols are available in the relevant DLL.
Yes, a .lib file is merely a collection of .obj files. Nothing was done with the content of the .obj files, the best analogy is a .zip archive. Yes, you can delete the .obj files after creating the .lib since the .lib contains a verbatim copy of the .obj files.
Beware that if you use a .lib to distribute your product then you'll typically have to create 4 of them. Debug vs Release build and the two flavors of CRT (/MT vs /MD).
精彩评论