(1)What is the difference between dynamic link library, shared library and static library?
(2)And what is the difference between source files and header files?
What is the connection between (1) and (2)?
Is there a good books for (1) and (2)?
(1) A dynamically linked library (.dll on windows, .so on linux) is a separate file that contains executable code that your program may call. Usually the OS loads this library and many programs can use it at the same time without needing to load another copy into memory. They are also useful in making your program more modular, because you can swap the library out for a newer or different version provided that the same functions are implemented in the library. (I think they are also called shared librarys, could someone correct me if I'm wrong?)
A static library becomes a part of the final executable of your program and cannot be changed after compilation.
(2) A source file contains the source code. A header file contains the required function/class names (declarations?) to use the source code. e.g:
A source file: (some.cpp)
1 2 3 4 5 6 7 8
#include "some.h"
void functionName(int a, float b){
//code here
}
void className::classFunction(double a){
//code here
}
A header file: (some.h)
1 2 3 4 5
void functionName(int a, float b);
class className{
public:
void classFunction(double a);
};
(3) A header file is required for a library to use the library, otherwise the compiler will not know what is defined in the library.
// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the shared library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.
extern"C"
{
// A function adding two integers and returning the result
int SampleAddInt(int i1, int i2)
{
return i1 + i2;
}
// A function doing nothing ;)
void SampleFunction1()
{
// insert code here
}
// A function always returning zero
int SampleFunction2()
{
// insert code here
return 0;
}
}