Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation errors for C++17 <filesystem> on MinGW

I want to play around with the new filesystem library that's now apart of the C++17 standard, however I can't get things to compile.

Things I've already tried:

  • Updating MinGW to 8.2.0
  • Compiling with g++ -std=c++17 test.cpp -o test
  • Adding -lstdc++fs to the compilation (this does not work, I get the error c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++fs)
  • Using <filesystem> as well as <experimental\filesystem>

Here is my simple test code just to try and get things compiling:

#include <iostream>
#include <filesystem>

using namespace std;

int main(int argc, char* argv[]) {
  return 0;
}

and compiling with g++ -std=c++17 test.cpp -o test

With this I get the error(s):

In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\filesystem:37,
                 from test.cpp:2:
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
    || (__p.has_root_name() && __p.root_name() != root_name()))
                               ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
                 from test.cpp:1:

... many more errors ...

c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:603:7: note: suggested alternative: 'string_view'
       string_type __tmp;
       ^~~~~~~~~~~
       string_view
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:604:45: error: '__tmp' was not declared in this scope
       if (__str_codecvt_in(__first, __last, __tmp, __cvt))

Does anyone else have any suggestions? It seems like most people are solving this by adding -lstdc++fs to compilation, but like I said that doesn't work for me.

Thanks!

like image 510
Sam McC Avatar asked Feb 10 '19 17:02

Sam McC


People also ask

Is MinGW same for C and C++?

MinGW is a native C/C++ compiler(GCC) which have free distributable import libraries and header files for building native Windows applications.

Can MinGW compile C++?

MinGW is a compiler system based on the GNU GCC and Binutils projects that compiles and links code to be run on Win32 (Windows) systems. It provides C, C++ and Fortran compilers plus other related tools.

Does MinGW include GCC?

MinGW includes a port of the GNU Compiler Collection (GCC), GNU Binutils for Windows (assembler, linker, archive manager), a set of freely distributable Windows specific header files and static import libraries which enable the use of the Windows API, a Windows native build of the GNU Project's GNU Debugger, and ...


1 Answers

The issue is with the mingw and gcc/g++ 8 branch itself, not with the compiler flags or pre-processor directives. The bug is open here.

Try using stable mingw-w64-7.x releases with #include <experimental/filesystem> directive and -lstdc++fs -std=c++17 flags. This will work for now, or otherwise wait for v9.1.0.

On experimental channel you need to use std::experimental::filesystem instead of std::filesystem.


If you don't want to go with experimental features, switch to MSYS2. It has v10.2.0-6 of gcc available as of Jan 2021.

like image 100
brc-dd Avatar answered Oct 09 '22 03:10

brc-dd