6

考虑这个最小的例子:

#include <filesystem>
#include <iostream>

int main()
{
    std::cout << std::filesystem::current_path() << '\n';
}

它在 GCC 9.2 中按预期工作,但 Clang 8.0.1 拒绝编译<filesystem>头文件(来自 GCC 9.2 的 libstdc++):

# clang++ 1.cpp -std=c++17
In file included from 1.cpp:1:
In file included from Z:\Lander\msys2\mingw64\include\c++\9.2.0\filesystem:37:
Z:\Lander\msys2\mingw64\include\c++\9.2.0\bits/fs_path.h:636:31: error: invalid use of incomplete
      type 'std::filesystem::__cxx11::filesystem_error'
      _GLIBCXX_THROW_OR_ABORT(filesystem_error(
                              ^~~~~~~~~~~~~~~~~
Z:\Lander\msys2\mingw64\include\c++\9.2.0\x86_64-w64-mingw32\bits/c++config.h:177:49: note: expanded
      from macro '_GLIBCXX_THROW_OR_ABORT'
#  define _GLIBCXX_THROW_OR_ABORT(_EXC) (throw (_EXC))
                                                ^~~~
Z:\Lander\msys2\mingw64\include\c++\9.2.0\bits/fs_fwd.h:61:9: note: forward declaration of
      'std::filesystem::__cxx11::filesystem_error'
  class filesystem_error;
        ^
1 error generated.

它是 Clang 错误还是 libstdc++ 错误?

我在 MSYS2 错误跟踪器上找到了这个错误报告,但那里没有有用的信息。

<filesystem>在我们等待官方修复时,有没有办法修补标头以消除此错误?


我在 Windows 上。我正在使用 MSYS2 包中提供的最新 GCC 和 Clang。

GCC 标识为:

# g++ --version
g++.exe (Rev2, Built by MSYS2 project) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Clang 标识为:

# clang++ --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: Z:\Lander\msys2\mingw64\bin

Clang 使用这个 GCC 附带的 libstdc++。

4

1 回答 1

11

这个问题可以通过打补丁来解决<msys2_path>/mingw64/include/c++/9.2.0/bits/fs_path.h

在第 666-692 行,定义了class filesystem_error. 它必须上移到第 614 行,正好在u8path().


我认为这是一个 libstdc++ 错误。我已经在这里报告了。

class filesystem_error在 中多次bits/fs_path.h使用,每次使用都低于定义,但有问题的第 636 行除外。

该行包含在 中#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS,所以我猜 Clang 开发人员不会在 Windows 上运行 libstdc++ 兼容性测试。


UPD:这在 GCC 9.3 中已修复。

于 2019-09-16T20:28:40.833 回答