What is the use of .lib file?

Started by
6 comments, last by Aardvajk 13 years, 10 months ago
Why can't we just import .h files instead of .lib ?
THX
Advertisement
Because headers normally only contain declarations and no definitions. Thereby, libraries are precompiled, which is a huge advantage with bigger projects. They only need to be linked.

Correct me if I'm wrong though, I'm learning just as well ;)
What do I expect? A young man's quest to defeat an evil sorceror while discovering the truth of his origins. A plucky youngster attended by her brutish guardian. A powerful artifact which has been broken into a small number of artifactlets distributed around the world.What do I want? Fewer damn cliches. - Sneftel
the implementation can be put in .cpp
a pair of .h and .cpp should be work well....


Companies don't want to distribute the source code. A compiled library, while essentially containing the same information, is much harder to extract information from. It also means you get used to treating the library as a black box that "just works" (hopefully).

There are downsides, bugs in the library cannot be fixed by library clients and if the code is poorly documented then it can be useful to have access to the source.

Finally, library files allow you to use code that wasn't written in C++. For instance, C99 code (if the interface is compatible) or other langauges that might compile to lib files.

Which libraries are you using? For many common libraries, the source is freely available. The compiled library files are just a convenience for those who don't need the source.
cpp files need to be compiled, increasing compile time. They're also human readable unlike .lib files... Also some benefits to dynamic linking as well.
Quote:Original post by s0962552
the implementation can be put in .cpp
a pair of .h and .cpp should be work well....


A pair of those would work fine but many libraries are huge.

When you compile a cpp file it gets compiled into a .obj file, those are then linked together into a .exe (or what ever it is your making). a lib is kind of the same, its been precompiled into a .lib. When you make a project you will have some .cpp which are compiled into the obj files and finally its all linked together (obj and lib) into an executable.

If you had to have the cpp (and other) files for everyhting it would be absolutly crazy abouts to put into a project :/ (alot of code is also stored in dlls etc).

Someone correct me if I've got that process wrong, I'm no expert myself.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

THX all of you.
I have one more things to ask.
.lib files still need to work with header files?

Quote:Original post by s0962552
THX all of you.
I have one more things to ask.
.lib files still need to work with header files?


Normally, yes. They don't technically need header files due to C and C++ archaic copy-paste based module system (a .h is literally copy-pasted into your source when you #include it) but it is the usual way of keeping class definitions, function prototypes and extern-ed variables consistent.

A more modern set of languages like the .NET group just need the .dll assembly linked to the project, which is far nicer, but I doubt I'll live to see C++'s linkage system updated (and I'm only 35 :)).

This topic is closed to new replies.

Advertisement