使用 boost::filesystem 时出错
Error using boost::filesystem
我正在尝试将所有 .txt 文件读取到给定文件夹中,并且我正在尝试为此使用 Boost 库:
int FileLoad::ReadTxtFiles(const std::string folder){
int loadStatus = LOAD_OK;
// Check if given folder exists
if(boost::filesystem::is_directory(folder)){
// Iterate existing text files
boost::filesystem::directory_iterator end_iter;
for(boost::filesystem::directory_iterator dir_itr(folder);
dir_itr!=end_iter; dir_itr++){
boost::filesystem::path filePath;
try{
// Check if it is a file
if(boost::filesystem::is_regular_file(dir_itr->status())){
filePath = dir_itr->path();
// Check that it is .txt extension
std::string fileExtension =
dir_itr->path().extension().string(); // Case insensitive comparison
if(boost::iequals(fileExtension, ".txt")){
// Filename is the code used as id when the file text is loaded to a database
std::string fileName = dir_itr->path().stem().string();
std::istringstream is(fileName);
unsigned int entryId;
is >> entryId;
// Check if an entry with that code id currently exists
// at the database
if(!DATABASE::CheckIfEntryExists(entryId)){
// Process text file
loadStatus = ProcessFile(filePath.string());
}
}
}
}
catch(const std::exception& ex){
std::cerr << " [FILE] Error trying to open file " <<
filePath.string() << std::endl;
}
}
}
return loadStatus;
}
但我收到两个编译器错误:
undefined reference to `boost::filesystem3::path::extension() const'
undefined reference to `boost::filesystem3::path::stem() const'
我在 class 头文件中导入了以下内容:
#include "boost/algorithm/string.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
(其中不相关的,例如)
我做错了什么?
您必须 link 和 -lboost_filesystem -lboost_system
,才能解决 link 等错误
Boost 文件系统取决于这些库中可用的其他已编译组件
这些是 linker 错误,不是编译器错误。请 link 针对 boost 文件系统库和系统库,b/c 文件系统依赖于它。
我正在尝试将所有 .txt 文件读取到给定文件夹中,并且我正在尝试为此使用 Boost 库:
int FileLoad::ReadTxtFiles(const std::string folder){
int loadStatus = LOAD_OK;
// Check if given folder exists
if(boost::filesystem::is_directory(folder)){
// Iterate existing text files
boost::filesystem::directory_iterator end_iter;
for(boost::filesystem::directory_iterator dir_itr(folder);
dir_itr!=end_iter; dir_itr++){
boost::filesystem::path filePath;
try{
// Check if it is a file
if(boost::filesystem::is_regular_file(dir_itr->status())){
filePath = dir_itr->path();
// Check that it is .txt extension
std::string fileExtension =
dir_itr->path().extension().string(); // Case insensitive comparison
if(boost::iequals(fileExtension, ".txt")){
// Filename is the code used as id when the file text is loaded to a database
std::string fileName = dir_itr->path().stem().string();
std::istringstream is(fileName);
unsigned int entryId;
is >> entryId;
// Check if an entry with that code id currently exists
// at the database
if(!DATABASE::CheckIfEntryExists(entryId)){
// Process text file
loadStatus = ProcessFile(filePath.string());
}
}
}
}
catch(const std::exception& ex){
std::cerr << " [FILE] Error trying to open file " <<
filePath.string() << std::endl;
}
}
}
return loadStatus;
}
但我收到两个编译器错误:
undefined reference to `boost::filesystem3::path::extension() const'
undefined reference to `boost::filesystem3::path::stem() const'
我在 class 头文件中导入了以下内容:
#include "boost/algorithm/string.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
(其中不相关的,例如)
我做错了什么?
您必须 link 和 -lboost_filesystem -lboost_system
,才能解决 link 等错误
Boost 文件系统取决于这些库中可用的其他已编译组件
这些是 linker 错误,不是编译器错误。请 link 针对 boost 文件系统库和系统库,b/c 文件系统依赖于它。