xander... are you really sure you UNDERSTOOD what a library, and what a header is? It appears you didn't- please read this:
A
compiler (
http://en.wikipedia.org/wiki/Compiler ) translates source code into object code. Object code is very close to executable code.
In C and C++, before the compilation a
preprocessor (
http://en.wikipedia.org/wiki/Preprocessor )
modifies the source code. It transforms macros into source code, and replaces #include directives with the contents of the included header files.
After that, a
linker (
http://en.wikipedia.org/wiki/Linker_%28computing%29 ) is used to combine (link, but explaining a word with itself is bad style, or so I was told) the object files into a single executable.
That's the general gist of it.
A library in C and C++ contains object code. You do not
include object code, you
link it. However, so the symbols of the linked library are known within the source code, you need a
header that contains the symbol definitions.
Example: You have 3 files:
main.cpp contains the main function, and the general programming logic.
1 2 3 4 5 6 7 8
|
//main.cpp
#include "faculty.h"
int main(int argc, char** argv)
{
int a = 5;
int b = faculty(a);
return 0;
}
|
faculty.cpp contains a function to calculate the faculty of a number.
1 2 3 4 5 6 7 8 9 10 11
|
//faculty.cpp
include "faculty.h"
int faculty(int N)
{
int n=1;
for(int i=2; i<=N;i++)
{
n*=i;
}
return n;
}
|
faculty.h contains the signature of that function
int faculty(int);
Now, first the preprocessor:
#include "faculty.h"
Is replaced by the contents of faculty.h in main.cpp and faculty.cpp
main.cpp and faculty.cpp are compiled to the object files main.o and faculty.o respectively.
The linker is invoked, and links the object files main.o and faculty.o to the executable main.exe
A library contains compiled object code. To use it, you have to link the object code, and include the appropitiate header files.