[C++] Hướng dẫn tạo và sử dụng thư viện LIB và DLL

1. Tạo và sử dụng thư viện LIB:
    1.1 Tạo dự án DemoLib: là thư viện static lỉbrary --> Dự án này khi biên dịch sẽ sinh ra tệp tin DemoLib.lib
    File --> New --> Project --> Win32 --> Win32 Console Application --> Next --> (Static library + Empty project) --> Finish.

   1.2 Tạo tệp tin: DemoLib.h và DemoLib.cpp như sau:
      
//DemoLib.h
#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
int Add(int x, int y);
#endif   

//DemoLib.cpp
#include "DemoLib.h"
int Add(int x, int y)
{
      return x + y;
}

    1.3 Tạo dự án DemoUseLib: sẽ sử dụng các hàm trong thư viện DemoLib
         -  Tạo dự án DemoUseLib: Bấm chuột phải vào Solution trên visual studio 2008, chọn Add New project --> Win32 --> Win32 Console Application --> Next --> (Console application + Empty project) --> Finish.
         -  Tạo tệp tin main.cpp:

//main.cpp
#include <iostream>
#include "DemoLib.h"

using namespace std;

void main()
{
      printf("Add(2, 3) = %d\n", Add(2, 3));
      getchar();
}
 
    1.4 Setting để project DemoUseLib sử dụng được thư viện DemoLib ở trên: có 2 cách setting
         (1) Cách 1: Sử dụng references để link dự án DemoLib vào dự án DemoUseLib:
              - Click chuột phải vào project DemoUseLib --> chọn Property --> Common Properties  --> Chọn Framework and References --> click Add New Reference --> chọn dự án DemoLib --> chọn Ok
              - Chỉ đường dẫn tới tệp tin .h trong dự án DemoLib: Click chuột phải vào project DemoUseLib --> chọn Property --> C++ --> General --> Additional Include Directories --> trỏ đường dẫn tới hư mục chứa tệp tin *.h (chẳng hạn thêm: $(SolutionDir)\DemoLib, DemoLib là thư mục chứa tệp tin DemoLib.h)
   
         (2) Cách 2: Trở tới thư mục .Lib:
              - Trỏ đường dẫn tới thư mục sinh ra tệp tin DemoLib.lib: chọn Property --> Linker --> General --> Additional Library Directories --> $(SolutionDir)\Debug (trong đó Debug là thư mục chứa tệp tin DemoLib.lib)

              - Tên thư viện .Lib: Property --> Linker --> Input--> Additional Dependencies --> DemoLib.lib add thêm vào

              - Chỉ đường dẫn tới tệp tin .h trong dự án DemoLib: Click chuột phải vào project DemoUseLib --> chọn Property --> C++ --> General --> Additional Include Directories --> trỏ đường dẫn tới hư mục chứa tệp tin *.h (chẳng hạn thêm: $(SolutionDir)\DemoLib, DemoLib là thư mục chứa tệp tin DemoLib.h)

2. Tạo và sử dụng thư viện DLL:

  2.1 Tạo dự án thư viện động DemoDLL: là thư viện động -> kết quả khi build thư viện này sinh ra tệp tin DemoDLL.dll và DemoDLL.lib.
         + Tệp tin DemoDLL.dll sử dụng cho phía người dùng, tức là khi tệp tin .exe có dùng DemoDLL.dll thì thường là copy thư viện .dll cùng thư mục với tệp tin .exe. Nếu không có tệp tin .dll thì tệp tin .exe sẽ không chạy được.
          + Tệp tin DemoDLL.lib dùng để biên dịch chương trình.

    File --> New --> Project --> Win32 --> Win32 Console Application --> Next --> (chọn option DLL + Empty project) --> Finish.

  2.2 Tạo tệp tin: DemoDLL.h và DemoDLL.cpp như sau:
      
//DemoDLL.h
#ifdef DEMODLL_EXPORTS
#define DEMODLL_API __declspec(dllexport)//Để biên dịch export (1)
#else
#define DEMODLL_API __declspec(dllimport) //Để sử dụng import    (2)
#endif 

DEMODLL_API int Sub(int x, int y);

 - Lưu ý
        + Với đoạn code (1) để khi bên dịch chương trình sẽ export ra tệp tin .DLL và .Lib. Với biến DEMODLL_EXPORTS đã được định nghĩa trong Property --> C++ --> Preprocessor --> Trong phần Preprocessor Definitions.
        + Với đoạn code (2) để khi chương trình khác sử dụng thư viện .DLL và .Lib thì sẽ import vào.

//DemoDLL.cpp
#include "DemoDLL.h"

DEMODLL_API int Sub(int x, int y)
{
      return x - y;
}

    2.3 Tạo dự án DemoUseDLL: sẽ sử dụng các thư viện DemoLL.dll và DemoDLL.lib
         -  Tạo dự án DemoUseDLL: Bấm chuột phải vào Solution trên visual studio 2008, chọn Add New project --> Win32 --> Win32 Console Application --> Next --> (Console application + Empty project) --> Finish.
         -  Tạo tệp tin main.cpp:

//main.cpp
#include <iostream>
#include "DemoDLL.h"

using namespace std;

void main()
{
      printf("Sub(2, 3) = %d\n", Sub(2, 3));
      getchar();
}

    2.4 Setting để project DemoUseDLL sử dụng được thư viện DemoDLL ở trên:
              - Trở đường dẫn tới thư mục sinh ra tệp tin DemoDLL.lib: chọn Property --> Linker --> General --> Additional Library Directories --> $(SolutionDir)\Debug (trong đó Debug là thư mục chứa tệp tin DemoDLL.lib)

              - Tên thư viện DemoDLL.Lib: Property --> Linker --> Input--> Additional Dependencies --> DemoDLL.lib add thêm vào

              - Chỉ đường dẫn tới tệp tin DemoDLL.h trong dự án DemoDLL: Click chuột phải vào project DemoUseDLL --> chọn Property --> C++ --> General --> Additional Include Directories --> trỏ đường dẫn tới hư mục chứa tệp tin DemoDLL.h vì trong main.cpp include tệp tin này (chẳng hạn thêm: $(SolutionDir)\DemoDLL, DemoDLL là thư mục chứa tệp tin DemoDLL.h)

3. Xem demo ở đây:
    Link: https://drive.google.com/file/d/0B8tAQ0_sJKCSdHREYXViSVRITW8/edit?usp=sharing