1: //stdafx.h
2: #pragma once3: #define MATHLIB_EXPORT
1: //MathLib.h
2: #pragma once3:4: #ifdef MATHLIB_EXPORT5: #define MATHLIBAPI __declspec(dllexport)6: #else
7: #define MATHLIBAPI __declspec(dllimport)8: #endif9:10: //macro
11: #define PI 3.1414912:13: //Global variable
14: extern MATHLIBAPI int GlobalVariable;15:16: //Function
17: MATHLIBAPI int Add(int a,int b);18:19: //Class
20: class MATHLIBAPI Math
21: {22: public:
23: int Multiply(int a,int b);24: };
1: //MathLib.cpp
2: #include "stdafx.h"
3: #include "MathLib.h"
4:5: int GlobalVariable = 100;
6:7: int Add(int a,int b)8: {9: return a+b;
10: }11:12: int Math::Multiply(int a,int b)13: {14: return a*b;
15: }
测试代码:
1: #include "stdafx.h"
2: #include <iostream>3: using namespace std;4:5: #include "../MathLib/MathLib.h"
6: #pragma comment(lib,"../Debug/MathLib.lib")
7:8: int _tmain(int argc, _TCHAR* argv[])9: {10: cout<<"Pi = "<<PI<<endl;
11:12: cout<<"GlobalVariable = "<<GlobalVariable<<endl;
13:14: int a = 20,b = 30;
15: cout<<"a="<<a<<", "<<"b="<<b<<endl;16: cout<<"a+b = "<<Add(a,b)<<endl;
17:18: Math math;19: cout<<"a*b = "<<math.Multiply(a,b)<<endl;
20:21: return 0;
22: }