tim
tim

Reputation: 3

can't use filesystem in g++ / msys2 on windows

So, I am writing C++ code on my windows 10 machine. I generally use cmake (3.17.2) and minGW (g++ 9.3.0) I found out about "filesystem" and decided to use it, but kept getting the error that it was undefined. Apparently "filesystem" wasn't implemented on minGW until version 9 and at the time, I was using version 8. So, I upgraded to version 9. After upgrading, my program wouldn't compile because of a threading issue which I was able to resolve by installing msys2 and using its package manager to install the full toolchain.

My current problem is that when I query msys2 about it says it's installed:

$ pacboy -Ss filesystem

msys/filesystem 2020.02-2 (base) [installed]
    Base filesystem

but when I try to compile a program from command line, I get this:

   F:\things\software\Template-CMD>g++ main.cpp 
main.cpp: In function 'int main(int, char**)': main.cpp:5:10: error: 'std::filesystem' has not been declared
        5 |     std::filesystem::current_path();
          |          ^~~~~~~~~~

This is the output from compiling the following small test program consisting of one file, "main.cpp":

#include <iostream>
#include <filesystem>

int main(int, char**) {
    std::filesystem::current_path();
    std::cout << "Hello, world!\n";
}

I can use windows file search to confirm that a file named "filesystem" does exist at:

C:\msys64\mingw64\include\c++\9.3.0\filesystem

so I tried to copy it to just the include folder:

C:\msys64\mingw64\include\filesystem

but that didn't work either. My PATH has only one g++ compiler in it:

C:\msys64\mingw64\bin

To show that g++ is version 9:

F:\things\software\Template-CMD>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-9.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++ --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --enable-plugin --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 9.3.0 (Rev2, Built by MSYS2 project)

Upvotes: 0

Views: 2064

Answers (1)

David Grayson
David Grayson

Reputation: 87486

File system is a general term in computing which can mean different things in different contexts.

The MSYS2 package you found named filesystem is just a basic package for MSYS2 that installs a bunch of files and folders inside MSYS2. Like any other MSYS2 package, you can investigate filesystem to see exactly what it installs by running pacman -Ql filesystem. But this package is irrelevant to the problem at hand.

The library you load by writing #include <filesystem> inside a C++ program is part of the C++ standard library, and it helps you access the file systems of your computer. This part of the standard library was added in C++17, but GCC 9.3.0 defaults to using some older version of C++ when you don't specify what version to use. To use filesystem, you should compile your program with a command like this, which specifies that you want to use C++17:

g++ -std=gnu++17 main.cpp

Upvotes: 1

Related Questions