靜態lib的創建
1、新建Win32應用程序,在應用程序類型中選擇靜態庫,如下圖所示
2、在該工程中新建testLib.h文件,文件內容如下
#ifndef LIB_H
#define LIB_H
int CAdd(int x,int y);
#endif
3、新建testLib.cpp文件,內容如下
#include "stdafx.h"
#include "testLib.h"
int CAdd(int x,int y)
{
return x + y;
}
4、然後生成解決方案,在工程的Debug目錄下即可看到想要的.lib文件
靜態lib文件的使用
1、新建VS工程,在 工程-配置屬性-鏈接器-常規-附加庫目錄 中添加所要使用的lib文件的路徑。
2、然後在 工程-配置屬性-鏈接器-輸入-附加依賴項 中添加庫的名稱
3、在cpp文件中包含相應的頭文件
#include <iostream>
#include "testLib.h" //包含相應頭文件
using namespace std;
int main()
{
cout<<CAdd(3,5)<<endl;
return 0;
}