Clang 拒绝编译 libstdc++ 的 <filesystem> 头文件

Hol*_*Cat 6 c++ clang libstdc++ std-filesystem

考虑这个最小的例子:

#include <filesystem>
#include <iostream>

int main()
{
    std::cout << std::filesystem::current_path() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Clang 使用此 GCC 附带的 libstdc++。

Hol*_*Cat 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 中修复。