创建静态链接库

1、建立工程,选择win32 project

2、新建项目,如:staticLib.h和staticLib.cpp

在staticLib.h中添加代码:

#ifndef STATICLIB_H
#define STATICLIB_H
extern "C" int add(int, int);
#endif


在staticLib.cpp文件中加入代码:

#include "stdafx.h"
#include <Windows.h>
#include "..\staticLib\staticLib.h"
#pragma comment(lib, "..\\debug\\staticLib.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	printf(" 2 + 3 = %d\n", add(2,3));
	system("pause");
	return 0;
}


编译工程。

3、做测试程序,可以在源solution中再添加一个test工程,进行测试

#include "stdafx.h"
#include <Windows.h>
#include "..\staticLib\staticLib.h"
#pragma comment(lib, "..\\debug\\staticLib.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	printf(" 2 + 3 = %d\n", add(2,3));
	system("pause");
	return 0;
}


设置为setup project,编译执行。

 

 由于库文件不能单独执行,因而在按下F5(开始debug模式执行)或CTRL+F5(运行)执行时,会弹出对话框说无法执行。

 

通常有比上述做法更好的调试途径,那就是将库工程和应用工程(调用库的工程)放置在同一VC工作区,只对应用工程进行调试,在应用工程调用库中函数的语句处设置断点,执行后按下F11,这样就单步进入了库中的函数。第2节中的libTest和libCall工程就放在了同一工作区,其工程结构如图4所示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值