静态库lib(vs2005)
一,创建一个win32的lib项目,如下图:
二、添加两个文件libtest.h,libtest.cpp,如下:
//libtest.h
#ifndef LIB_H
#define LIB_H
extern "C" int add(int a,int b);
#endif
//libtest.cpp
#include "libtest.h"
int add(int a,int b)
{
return a + b;
}
三、创建一个调用主程序
在创建库工程中创建一个调用程序控制台调用程序
1、第一种调用方法
// libcall.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "libtest.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"1+2="<<add(1,2)<<endl;
return 0;
}
这种调用方式需对主程序进行配置,如下图:
1、添加include路径
2、添加附加库目录
3、添加被调用静态库dlltest.lib
2、第二种调用方法,直接把链接相关部分用代码指定。
// libcall1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "..//dlltest//libtest.h"
#include <iostream>
#pragma comment(lib,"..//debug//dlltest.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"1+2="<<add(1,2)<<endl;
return 0;
}