Clarification on libraries

Howdy, I have about almost 2 years of c++ under my belt, but I would just like to clarify some things that I haven't learned in depth. I don't expect full blown answers for this but a reference to maybe a website would help :P I wasn't really able to find anything on google that would clarify this for me.

What exactly is a library, a collection of header files that you can call within your main? I know there are static and dynamic libraries but this is something i haven't learned in depth. Do you use #include<> with them, or do you have to reference/add them into your program somehow? And where to people go to find libraries they need?

I'm not 100% sure if I'm asking the question correctly, if something is unclear to you just let me know and I'll try to clarify a little bit, and as I said before this is foggy for me so my wording might be a tad off.
You have to understand that #include<> just copy pastes text. Right? It copy pastes the content of the file you're including to the file in which you are including. Simple as that.

In the case of the more famous C++ libraries out there, like Boost or the STL itself, the content is pretty much templates and those can't effectively be separated into .h or .hpp and .cpp files. So they are all coded into .h or .hpp files.

You mentioned static (.lib) and dynamic (.dll) libraries.
You can compile your .cpp files into either of those. This is a way to hide the implementation from the library user. The user can no longer see the .cpp files. Only the .h files. So you have to provide a way to the linker to know where to find those .lib or .dll files and be able to link them to the .h or .hpp file. That is usually done through the settings of your favorite IDE. Complicated enough?

After the linker knows where the .lib or .dll files are (which is essentially the implementation or .cpp in disguise), you can call #include <...> and everything will work as expected. The linker will link the function prototypes from the header and the implementation in the .lib or .dll, just as it normally would do if it was a .cpp file.

Now, as I said above, the more popular libraries just use templates and those are all coded into header files. So they are not separated into .lib or .dll. That means you don't have to do any linking and just #include<> the needed header file.

You can find more about how static and dynamic libraries are created and how they are used and whatnot on wikipedia or your favorite website!

I hope that answers your question up to a point. Also you could say that a library is anything that is not the core language. Right? Anything that you have to #include is part of a library.
Last edited on

Now, as I said above, the more popular libraries just use templates and those are all
coded into header files. So they are not separated into .lib or .dll. That means you don't
have to do any linking and just #include<> the needed header file.

I don't think popularity of the library has something to do with definitions being inside .h or .lib/dll. Winsock, SDL, OpenGL and many more libraries require you to link them instead of .h having the function definitions.
Last edited on
Haha thanks guys, I never really went in depth with the basics as to why something was there (i.e. #include<>) They just really told us that it allows us to reference a file of some sort. I wanted a more definitive answer which is why I came here for clarification on some simple, yet, important terms that I should understand. Thanks again :)
That is actually a big problem of the educational system. They never tell you why something behaves like it does. It just does. On the first day they tell you:
1
2
3
4
5
6
#include <iostream> 
using namespace std;
 int main() 
{
     return 0;
 }
They never tell you how it works. And half of that code is even bad advice and bad practice. What can I say? I can rage and rant a long long time :D
Last edited on
@AcarX You're absolutely right. It's just that Boost came to my head when I thought "libraries". So I think I should settle on "open-source libraries" rather than "popular libraries".
when somebody says that he is using some C++ library, this usually means a library such as:
boost, gtkmm, crypto++, Qt etc...

these libraries provide a lot of "features" that implement (more or less) modern language idioms which you can use to rapidly write code. that is you are not required to use OS specific API's.

for example the standard c++ library (STL, <string>) and so on also goes into that group, the difference is that standard library comes pre-built with compiler as is a standard. while other libraries usually require that you build them separately ( compile them by using your default compiler ), and these are not standard.

once compiled you include headers just like any other, and instruct your linker to link against static lib or DLL. which you built.

Thanks man

And @sasauke , on top of telling us what to write, not why it is there, they told us to use system("pause"); as well :P I see what you mean about bad practices haha.

1
2
3
4
5
6
7
#include <iostream> 
using namespace std;
 int main() 
{
     system("pause");
     return 0;
 }


But yeah, I want to actually understand the basics rather than have it crammed into my head to write this, as compared to, knowing what to write, why, and its significance in the program.

Last edited on
Topic archived. No new replies allowed.