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所示。