如何写自己的lib文件并测试

1、在VS中新建一个win32工程,建立时选择静态库,如图:


2、也可新建一个win32项目,然后在工程——配置属性——常规中选择,如图:


3、新建一个GetImageName.h文件和一个GetImageName.cpp文件

GetImageName.h文件

#ifndef GET_IMAGE_NAME_H_
#define GET_IMAGE_NAME_H_
#include<io.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void getAllFiles(const string path,const string ext,vector<string>& files);
#endif
GetImageName.cpp 文件

#include "GetImageName.h"
void getAllFiles(const string path,const string ext,vector<string>& files)
{
	cout<<path<<endl;
	cout<<ext<<endl;
	//文件句柄
	long hFile = 0;
	struct _finddata_t fileInfo;
	string p;
	
	if((hFile = _findfirst(p.assign(path).append("\\*").append(ext).c_str(),&fileInfo))!=-1)
	{
		do
		{	
			files.push_back(fileInfo.name);
		}while(_findnext(hFile,&fileInfo)==0);
		_findclose(hFile);
	}
}

4、运行,就会在Debug下看到GetImageName.lib

5、测试自己的lib文件,一种方式是将GetImageName.h文件和GetImageName.lib文件放到自己的工程下,另一种是VC++目录下的包含目录和库目录将GetImageName.h文件和GetImageName.lib文件所在的目录添加上,如图


在连接器——输入加上GetImageName.lib,如图


6、测试代码

#include "GetImageName.h"

int main()
{
	vector<string> str;
	getAllFiles("C:\\Users\\admin\\Desktop\\DetectResult2.0_Samples",".jpg",str);
	for(int i=0;i<str.size();i++)
	{
		cout<<str[i]<<endl;
	}
	system("pause");
	return 0;
}

得到目录下所有jpg图像的文件名。

7、另一种使用lib文件方式

不在连接器——输入加上GetImageName.lib,而是在代码中加上

#pragma comment(lib,"GetImageName.lib")

路径要写对,我的lib文件就在工程下





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值