【问题标题】:Compilation errors for C++17 <filesystem> on MinGWMinGW 上 C++17 <filesystem> 的编译错误
【发布时间】:2019-07-04 06:25:58
【问题描述】:

我想使用新的 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
  • 使用&lt;filesystem&gt; 以及&lt;experimental\filesystem&gt;

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

#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:\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:

...更多错误...

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))

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

谢谢!

【问题讨论】:

  • 我认为你需要在编译命令中添加-lstdc++fs
  • OP 说他已经尝试过了,@Galik。
  • 我很确定 &lt;filesystem&gt; 现在不能与 MinGW 一起使用......
  • 您能否提供您使用的推荐包括 -lstdc++fs?库标志的位置很重要
  • @Fureeish 我开始怀疑这一点。我以为他们的文件说确实如此,但我一定是错的。你知道我应该改用什么吗?

标签: c++ filesystems c++17


【解决方案1】:

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

尝试使用带有#include &lt;experimental/filesystem&gt; 指令和-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。

【讨论】:

  • 非常感谢。我只发现了 2017 年的文件系统问题线程,并认为必须在 2021 年解决这个问题。可以让它用 mingw 7 和实验性编译。
猜你喜欢
  • 2019-10-06
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多