VC静态库的创建和使用

来源:互联网 发布:手机手柄连接软件 编辑:程序博客网 时间:2024/06/05 00:08
一、静态库项目的创建和生成。
打开VS2010,文件->新建->项目->windows应用程序,选择静态库项目(项目名称Cpplib)。

在项目中新建如下CMath类:

class CMath{public:int add(int a,int b){return a+b;}};
生成项目,可生成Cpplib.lib文件。

二、静态库的使用
1、隐式调用
在项目属性->VC++目录->库目录,添加该lib所在的目录,或者在项目属性->链接器->附件库目录中添加该静态库所在目录。
然后在项目属性->链接器->输入->附件依赖项,添加该库名称Cpplib.lib
即可进行使用lib库中的CMath类。
代码如下所示:

#include "stdafx.h"#include "../Cpplib/stdafx.h"int _tmain(int argc, _TCHAR* argv[]){CMath math;printf("%d",math.add(2,4));getchar();return 0;}
2、显式调用
在调用前用下面的语句显式导入静态库
#pragma comment(lib,"..\\lib\\Cpplib.lib")




源代码如下:

#include "stdafx.h"#include "../Cpplib/stdafx.h"#pragma comment(lib,"Cpplib.lib")int _tmain(int argc, _TCHAR* argv[]){CMath math;printf("%d",math.add(2,4));getchar();return 0;}




0 0
原创粉丝点击