0

I'm trying to use the filesystem library and it's not working I need help about compiling this.

I tried to change the included file and I updated my compiler but nothing works

here are the inclusions I made

#include <experimental/filesystem>
namespace fs = std::filesystem;

I compile the cpp file with this command

g++ -Wall -c indexation_fichier.cpp

I get this error

indexation_fichier.cpp:5:10: fatal error: experimental/filesystem: No such file or directory
 #include <experimental/filesystem>
      ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

and here is my compiler version

g++ (MinGW.org GCC-8.2.0-1) 8.2.0

when I type

g++ --version

I want to know what is wrong and what I need to do to make this library work because I need it for my project. thanks.

  • 2
    Try leaving out the "experimental/". The filesystem library was included in standard C++ in the 2017 version. – Pete Becker Dec 31 '18 at 19:01
  • Possible duplicate of [Why can't I use with g++ 4.9.2?](https://stackoverflow.com/questions/30103209/why-cant-i-use-experimental-filesystem-with-g-4-9-2) – Mike Lui Dec 31 '18 at 19:53
  • Possible duplicate of [Link errors using members in C++17](https://stackoverflow.com/questions/48729328/link-errors-using-filesystem-members-in-c17) – Mike Lui Dec 31 '18 at 19:59
  • @PeteBecker I tried and it works thank you so much – Mister onsépatro Jan 01 '19 at 18:39

1 Answers1

1

You can either compile your code using -lstdc++fs flag OR like @pete mentioned in the comment: remove experimental, as it is now part of standard C++17.

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main(){

     fs::path pathToShow(fs::current_path());

     std::cout << "exists() = " << fs::exists(pathToShow) << "\n"
     << "root_name() = " << pathToShow.root_name() << "\n"
     << "root_path() = " << pathToShow.root_path() << "\n"
     << "relative_path() = " << pathToShow.relative_path() << "\n"
     << "parent_path() = " << pathToShow.parent_path() << "\n"
     << "filename() = " << pathToShow.filename() << "\n"
     << "stem() = " << pathToShow.stem() << "\n"
     << "extension() = " << pathToShow.extension() << "\n";
        return 0;
}

and then something like g++ -o fs filesystem.cpp will work fine.