Different libraries

(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.
Last edited on
I see this difference between dynamic link library and shared library in codeblocks.
dynamic link library:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

main.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__ 


shared library:
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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;
    }
}

Looking at http://stackoverflow.com/questions/9688200/difference-between-shared-objects-so-static-libraries-a-and-dlls-so it looks like dynamicly linked libraries and shared libraries are the same, just a different name. You can look at the link (second answer might be more helpful) for more info.
Thank you.
Topic archived. No new replies allowed.