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文件就在工程下