MinGW 上 C++17 <filesystem> 的编译错误

Sam*_*McC 9 c++ filesystems c++17

我想使用filesystem现在属于 C++17 标准的新库,但是我无法编译。

我已经尝试过的事情:

  • 将 MinGW 更新到 8.2.0
  • 编译 g++ -std=c++17 test.cpp -o test
  • 添加-lstdc++fs到编译(这不起作用,我收到错误c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++fs
  • 使用<filesystem>以及<experimental\filesystem>

这是我的简单测试代码,只是为了尝试编译:

#include <iostream>
#include <filesystem>

using namespace std;

int main(int argc, char* argv[]) {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

并编译 g++ -std=c++17 test.cpp -o test

有了这个,我得到了错误:

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:
Run Code Online (Sandbox Code Playgroud)

...更多错误...

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))
Run Code Online (Sandbox Code Playgroud)

还有其他人有什么建议吗?似乎大多数人都通过添加-lstdc++fs到编译来解决这个问题,但就像我说的那样对我不起作用。

谢谢!

brc*_*-dd 10

问题在于 mingw 和 gcc/g++ 8 分支本身,而不是编译器标志或预处理器指令。错误在这里打开。

尝试使用mingw-w64-7.x带有#include <experimental/filesystem>指令和-lstdc++fs -std=c++17标志的稳定版本。这现在可以使用,或者等待 v9.1.0。

experimental频道上,您需要使用std::experimental::filesystem而不是std::filesystem.


如果您不想使用实验性功能,请切换到MSYS2。截至 2021 年 1 月,它具有 v10.2.0-6 的gcc