What is the need of libraries?

If all libraries for c++ are written in c++ what is the need to build the source files into a library and then use them?

It would make sense to build libraries if libraries for c++ could be written in other languages.
The alternative is to write out the source code yourself every time you wanted to do anything. Do you really want to have to write it out yourself every time? Can you imagine how difficult it would be to get started programming if you had to write the entire thing yourself? All the code to interact with hardware, the code to get anything on screen, all of it....

Even if you did want to do that, compiling these things can takes hours. Even days isn't unheard of with really big code bases. Do you really want to have to wait hours for everything to be recompiled just because you made a tiny change in a single source file? If you've got libraries that you already compiled, they don't need to be recompiled. You just recompile the code that changed.

What if you want to sell your clever code? If libraries didn't exist you'd have give people the actual source code. You might as well put up a big sign saying "pirate software available here".

How about if you use the same functions in many different programs. Right now, we use dynamic libraries. What a drag it would be to have to compile them over and over again into each different program. Each program would be bloated with all this duplication, and if we ever improved the code, we'd have to replace it in every program, and rebuild and recompile. With a dynamic library, we can simply replace that one dynamic library and every program gets access to the new functions.
Last edited on
Thanks for this nice explanation.
One another question.
Could you tell if we can make libraries for c++ in a different language?

My perception is that just like different language compilers translate the source codes into a common executable format(like .exe) the object modules built by them should also be similar, and hence inter-usable. What do you say?
That's a great answer, Moschops! So helpful, you are. Lol.
Topic archived. No new replies allowed.