0

I'm trying to read all .txt files into a given folder, and I am trying to use Boost libraries for that:

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;
}

But I am reeiving two compiler errors:

undefined reference to `boost::filesystem3::path::extension() const'
undefined reference to `boost::filesystem3::path::stem() const'

I have the following imports into the class header file:

#include "boost/algorithm/string.hpp"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"

(Among others which are not relevant, such as )

What am I doing wrong?

Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

2 Answers2

2

You will have to link with -lboost_filesystem -lboost_system, to resolve those linker errors

Boost filesystem depends on other compiled component available in those libraries

P0W
  • 46,614
  • 9
  • 72
  • 119
2

Those are linker errors, not compiler errors. Please link against the boost filesystem library and the system library as well, b/c filesystem depends on it.

FRob
  • 3,883
  • 2
  • 27
  • 40