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

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

本文关键字:lt filesystem gt 错误 C++17 编译 MinGW      更新时间:2023-10-16

我想尝试使用现在与 C++17 标准分开的新filesystem库,但我无法编译东西。

我已经尝试过的事情:

  • 将 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><experimentalfilesystem>

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

#include <iostream>
#include <filesystem>
using namespace std;
int main(int argc, char* argv[]) {
  return 0;
}

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

有了这个,我得到错误:

In file included from c:mingwlibgccmingw328.2.0includec++filesystem:37,
                 from test.cpp:2:
c:mingwlibgccmingw328.2.0includec++bitsfs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
c:mingwlibgccmingw328.2.0includec++bitsfs_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:mingwlibgccmingw328.2.0includec++iosfwd:40,
                 from c:mingwlibgccmingw328.2.0includec++ios:38,
                 from c:mingwlibgccmingw328.2.0includec++ostream:38,
                 from c:mingwlibgccmingw328.2.0includec++iostream:39,
                 from test.cpp:1:

。还有更多错误...

c:mingwlibgccmingw328.2.0includec++bitsfs_path.h:603:7: note: suggested alternative: 'string_view'
       string_type __tmp;
       ^~~~~~~~~~~
       string_view
c:mingwlibgccmingw328.2.0includec++bitsfs_path.h:604:45: error: '__tmp' was not declared in this scope
       if (__str_codecvt_in(__first, __last, __tmp, __cvt))

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

谢谢!

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

尝试使用带有#include <experimental/filesystem>指令和-lstdc++fs -std=c++17标志的稳定mingw-w64-7.x版本。这将暂时有效,否则等待v9.1.0。

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

<小时 />

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