设为首页 加入收藏

TOP

VC中创建DLL,导出全局变量,函数和类(隐式调用)
2014-11-23 21:30:34 】 浏览:578
Tags:创建 DLL 导出 全局 变量 函数 调用

一.创建DLL
1.在VC中新建一个Win32空项目MathLib;
2.添加预编译头文件stdafx.h,定义导入导出控制符号:
1: //stdafx.h

2: #pragma once

3: #define MATHLIB_EXPORT

3.添加包含要导出的全局变量,函数和类的头文件MathLib.h:
1: //MathLib.h

2: #pragma once

3:

4: #ifdef MATHLIB_EXPORT

5: #define MATHLIBAPI __declspec(dllexport)

6: #else

7: #define MATHLIBAPI __declspec(dllimport)

8: #endif

9:

10: //macro

11: #define PI 3.14149

12:

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: };

4.添加所导出元素的实现文件MathLib.cpp
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: }

二,测试所创建的DLL
测试代码:
1: #include "stdafx.h"

2: #include

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 = "<

11:

12: cout<<"GlobalVariable = "<

13:

14: int a = 20,b = 30;

15: cout<<"a="<

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC获取打印机与打印作业的状态 下一篇使用多个定时器

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目