動態庫
//pch.h
#ifndef PCH_H
#define PCH_H
#include "framework.h"
#endif //PCH_H
#ifdef IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport)
#endif
IMPORT_DLL int add(int a, int b);
IMPORT_DLL int minus(int a, int b);
IMPORT_DLL int multiply(int a, int b);
IMPORT_DLL double divide(int a, int b);
// dllmain.cpp : 定義 DLL 應用程序的入口點。
#include "pch.h"
int add(int a, int b)
{ return a + b; }
int minus(int a, int b)
{ return a - b; }
int multiply(int a, int b)
{ return a * b; }
double divide(int a, int b)
{ double m = (double)a / b; return m; }
// test20199321.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
#include <iostream>
#include<windows.h>
int main()
{
HINSTANCE hDllInst;
hDllInst = LoadLibrary(L"20199321.dll"); //調用DLL
typedef int(*PLUSFUNC)(int a, int b); //后邊為參數,前面為返回值
PLUSFUNC plus_str = (PLUSFUNC)GetProcAddress(hDllInst, "add"); //GetProcAddress為獲取該函數的地址
std::cout << plus_str(1,2);
}

靜態庫
//pch.h
#ifndef __PCH__
#define __PCH__
extern int add(int a, int b);//extern關鍵字說明這是一個外部函數,這個函數不由自己實現,而是外部的庫實現的,以便鏈接器進行鏈接
extern int minus(int a, int b);
extern int multiply(int a, int b);
extern double divide (int a, int b);
#endif
// 20199321lib.cpp : 定義靜態庫的函數。
//
#include "pch.h"
#include "framework.h"
int add(int a, int b)
{ return a + b; }
int minus(int a, int b)
{ return a - b; }
int multiply(int a, int b)
{ return a * b; }
double divide(int a, int b)
{ double m = (double)a / b; return m; }
#include<iostream>
#include"pch.h"
#pragma comment (lib,"20199321lib.lib")
using namespace std;int main()
{ int a=3,b=18; int c;
c=add(a,b); cout << c << endl; return 0;}
