C++ 模块文件系统和 iostream 崩溃 g++

C++ modules filesystem and iostream crashes g++

我有以下程序:

import <iostream>;
import <filesystem>;

namespace fs = std::filesystem;

int main(int argc, char* argv[]){
    fs::path input = "./";
    std::cout << input;
    return 0;
}

我用(g++版本11.1.0)编译它:

g++ -c -std=c++20 -fmodules-ts -x c++-system-header filesystem iostream
g++ -std=c++20 -fmodules-ts -o test test.cpp

我收到一条非常神秘的消息:

during RTL pass: expand
In file included from /usr/include/c++/11.1.0/string:55,
                 from /usr/include/c++/11.1.0/stdexcept:39,
                 from /usr/include/c++/11.1.0/system_error:41,
                 from /usr/include/c++/11.1.0/bits/fs_fwd.h:35,
                 from /usr/include/c++/11.1.0/filesystem:44,
of module /usr/include/c++/11.1.0/filesystem, imported at test.cpp:2:
/usr/include/c++/11.1.0/bits/basic_string.h: In static member function ‘static std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_type std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_to_string_view(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’:
/usr/include/c++/11.1.0/bits/basic_string.h:126:16: internal compiler error: in make_decl_rtl, at varasm.c:1418
  126 |       { return __svt; }
      |                ^~~~~
0x1797368 internal_error(char const*, ...)
        ???:0
0x67f8f9 fancy_abort(char const*, int, char const*)
        ???:0
0xa1cdef expand_expr_real_1(tree_node*, rtx_def*, machine_mode, expand_modifier, rtx_def**, bool)
        ???:0
0xa26f9f store_expr(tree_node*, rtx_def*, int, bool, bool)
        ???:0
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://bugs.archlinux.org/> for instructions.

令我困惑的是,在删除 iostreamstd::cout 行后,程序编译得很好。

此外,如果我从 import 切换到老式的 #include,程序也能正常工作。

我是不是导入模块有误? 这是编译器错误吗?

编译器对模块的支持仍处于早期阶段。但是,如果 header 无法编译为 header 单元,那么您仍然可以改为包含它:

import <iostream>;
#include <filesystem>

namespace fs = std::filesystem;

int main(int argc, char* argv[]){
    fs::path input = "./";
    std::cout << input;
    return 0;
}

这可能是我们目前能做的最好的了。如果您的依赖项列表失控,您也可以同时尝试预编译 headers。