Red Squirrel
[H]F Junkie
- Joined
- Nov 29, 2009
- Messages
- 9,223
Oh geez, I think you need to go read some of the basics of compiling and linking. A header merely provides signatures of what the library contains (not 100% true, but true enough here). To steal from this stackoverflow answer, "The header is a phone number you can call, while the library is the actual person you can reach there!"
Extending that a bit to give you a super short crash course in what happens when you "build", there are two main phases to a build, compiling and linking. I'll skip preprocessing, how template metaprogramming is compiled, etc. The short version of compiling: taking various bits of source code, e.g. C/C++, and transforming them into some output format, e.g. binary machine code. The short version of linking, biased towards statically linking: taking the various output format pieces and smashing them together in such a way that references between the various pieces may be resolved within the self-contained binary file.
Extending the phone number/person you call analogy, at the end of compiling you have binary code that says "when I want to use so and so function I call this phone number with this extension to reach them". Linking is like moving all of you into the same building, then replacing that phone number with their extension alone so you can call them within your building. Since the header just lets you know that there's a phone number to reach someone, it doesn't do you any good to just have that if there isn't actually someone you can reach.
This is a very abbreviated view of the process and not 100% technically correct, but hopefully it helps you start understanding why you need a library (not even sure what you mean by that - pure c/c++ code? pure machine code? pure intermediate language code?). If you mean "why can't I just copy their c/c++ source code and use it directly in my program - you can, but the point of a library is to give you a self-contained unit that has certain known points you may contact and use, hiding you from what's going on inside that unit.
I'd strongly suggest reading up about the phases of building an executable before mucking about further. It will serve to help you significantly in any future build issues you encounter.
I get that part, but what I'm trying to understand is why can't the stuff in the library be performed in regular code?
Ex: Say I want to write a header/class that does XYZ, I can write it all out in code, and ship it as a single header file (or a folder with multiple header/cpp files if I want to organize it better) that has everything required in it for it to work. No "install" required, just include the main header in your program and that header will include what it needs. Typically I create a .h and .cpp but I'm not even talking about that right now, as technically you can just put it all in one if you really wanted to. I know the .h that comes with libs still need the libs, but why can't they impliment what the libs do, in the .h file or other file that is called by the h file so that it's stand alone?
What does a library do special, that can't be done by simply writing it in a single set of header files so that everything needed for XYZ to work is written in normal code?
Say I'm a dev that works for MySQL, so I want to make a connector so people can use it, what stops me from simply implementing the MySQL protocol entirely in C++ and simply ship the code directly as a set of header files instead of making a lib? Do libs make certain special system calls that can't be done in plain C/C++?
C++ tutorials never really talk about the lib stuff. College definitely didn't talk about any of that either.