vs2015写c语言动态库,C/C++(2)VS2015生成并使用自定义动态库dll/静态库lib

动态库:

1.新建win32控制台应用程序DLLTEST,选中DLL(D)、预编译头(P)、导出符号(X)、自动生成了如下文件:

//DLLTEST.h文件

// 下列 ifdef 块是创建使从 DLL 导出更简单的

// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DLLTEST_EXPORTS

// 符号编译的。在使用此 DLL 的

// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将

// DLLTEST_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的

// 符号视为是被导出的。

#ifdef DLLTEST_EXPORTS

#define DLLTEST_API __declspec(dllexport)

#else

#define DLLTEST_API __declspec(dllimport)

#endif

// 此类是从 DLLTEST.dll 导出的

class DLLTEST_API CDLLTEST {

public:

CDLLTEST(void);

// TODO: 在此添加您的方法。

};

extern DLLTEST_API int nDLLTEST;

DLLTEST_API int fnDLLTEST(void);1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//DLLTEST.cpp文件

// DLLTEST.cpp : 定义 DLL 应用程序的导出函数。

//

#include "stdafx.h"

#include "DLLTEST.h"

// 这是导出变量的一个示例

DLLTEST_API int nDLLTEST=0;

// 这是导出函数的一个示例。

DLLTEST_API int fnDLLTEST(void)

{

return 42;

}

// 这是已导出类的构造函数。

// 有关类定义的信息,请参阅 DLLTEST.h

CDLLTEST::CDLLTEST()

{

return;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

2.类比着添加自定义的变量、函数和类,具体如下:

//DLLTEST.h文件

// 下列 ifdef 块是创建使从 DLL 导出更简单的

// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 DLLTEST_EXPORTS

// 符号编译的。在使用此 DLL 的

// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将

// DLLTEST_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的

// 符号视为是被导出的。

#ifdef DLLTEST_EXPORTS

#define DLLTEST_API __declspec(dllexport)

#else

#define DLLTEST_API __declspec(dllimport)

#endif

#include

using namespace std;

// 此类是从 DLLTEST.dll 导出的

class DLLTEST_API CDLLTEST { //导出的类

public:

CDLLTEST(void);

// TODO: 在此添加您的方法。

int mul(int x, int y);

double b;

};

class DLLTEST_API MyDLL {

public:

int a;

MyDLL();

int add(int x, int y);

};

extern DLLTEST_API int nDLLTEST; //导出的变量

extern DLLTEST_API double mydll;

DLLTEST_API int fnDLLTEST(void); //导出的函数

DLLTEST_API int sub(int x, int y);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

34

35

36

37

38

//DLLTEST.cpp文件

// DLLTEST.cpp : 定义 DLL 应用程序的导出函数。

#include "stdafx.h"

#include "DLLTEST.h"

// 这是导出变量的一个示例

DLLTEST_API int nDLLTEST=0;

DLLTEST_API double mydll = 0.999;

// 这是导出函数的一个示例。

DLLTEST_API int fnDLLTEST(void)

{

return 42;

}

DLLTEST_API int sub(int x, int y)

{

return x - y;

}

// 这是已导出类的构造函数。

// 有关类定义的信息,请参阅 DLLTEST.h

CDLLTEST::CDLLTEST()

{

b = 0.111;

return;

}

int CDLLTEST::mul(int x, int y)

{

return x * y;

}

MyDLL::MyDLL()

{

a = 555;

cout << "this is the constructor of my DLL!" << endl;

}

int MyDLL::add(int x, int y)

{

return x + y;

}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

34

35

36

37

38

39

40

41

42

43

44

3.分别在debug和release模式下生成库文件DLLTEST.dll和DLLTEST.lib.

4.新建测试工程:win32控制台应用程序Test,将debug(release下同样)模式下生成的库文件DLLTEST.dll复制到测试工程目录下,

将生成的库文件DLLTEST.lib和头文件DLLTEST.h复制到测试工程下的新建目录DLLTEST下,添加main.cpp文件,

测试代码如下:

#include

using namespace std;

#include "DLLTEST\DLLTEST.h"

#pragma comment(lib,"DLLTEST\\DLLTEST.lib")

int main() {

CDLLTEST cd;

cout << "CDLLTEST -> b = " << cd.b << endl;

cout << "CDLLTEST -> mul(2, 3) = " << cd.mul(2, 3) << endl;

MyDLL md;

cout << "MyDLL -> a = " << md.a << endl;

cout << "MyDLL -> add(2, 3) = " << md.add(2, 3) << endl;

cout << "DLL:mydll = " << mydll << endl;

cout << "DLL:nDLLTEST = " << nDLLTEST << endl;

cout << "DLL:fnDLLTEST = " << fnDLLTEST() << endl;

cout << "DLL:sub(2, 3) = " << sub(2, 3) << endl;

return 0;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

5.运行,输出如下ok:

CDLLTEST -> b = 0.111

CDLLTEST -> mul(2, 3) = 6

this is the constructor of my DLL!

MyDLL -> a = 555

MyDLL -> add(2, 3) = 5

DLL:mydll = 0.999

DLL:nDLLTEST = 0

DLL:fnDLLTEST = 42

DLL:sub(2, 3) = -1

静态库:

1.新建静态库工程LIBTEST,添加代码如下:

//LIBTEST.h

#pragma once

class LIBTEST

{

public:

LIBTEST();

int add(int x, int y);

~LIBTEST();

};1

2

3

4

5

6

7

8

9

//LIBTEST.cpp

#include "LIBTEST.h"

LIBTEST::LIBTEST()

{

}

int LIBTEST::add(int x, int y)

{

return x + y;

}

LIBTEST::~LIBTEST()

{

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

2.分别在debug和release模式下生成库文件LIBTEST.lib,将debug下生成的库文件修改名字为LIBTESTd.lib.

3.将库文件LIBTEST.lib、LIBTESTd.lib和头文件LIBTEST.h拷贝到测试工程目录下,编写测试程序

// main.cpp

#include

#include "LIBTEST/LIBTEST.h"

#ifdef _DEBUG

#pragma comment(lib, "LIBTEST/LIBTESTd.lib")

#else

#pragma comment(lib, "LIBTEST/LIBTEST.lib")

#endif //

using namespace std;

int main() {

LIBTEST lt;

cout << lt.add(1, 2) << endl;

return 0;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

4.分别在debug和release模式下运行即ok.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值