【问题标题】:How to use modules in c++ 20 using g++?如何使用 g++ 在 c++ 20 中使用模块?
【发布时间】:2021-07-05 21:48:07
【问题描述】:

我阅读了此链接 https://gcc.gnu.org/wiki/cxx-modules 并尝试从该网站复制以下示例。我已经知道这个编译器部分支持模块系统。 (注意:我用的是windows)

// hello.cc
module;
#include <iostream>
#include <string_view>
export module hello;
export void greeter(std::string_view const &name) {
  std::cout << "Hello " << name << "!\n";
}
// main.cc
import hello;
int main() {
  greeter("world");
  return 0;
}
// Should print "Hello world!" 

我目前有 GCC 11.0.1 快照,并尝试使用以下参数编译这些文件: g++ -std=gnu++2b -Wall -fmodules-ts hello.cc main.cc 和编译器给了我这些错误:

hello.cc:6:8: internal compiler error: in get_cxx_dialect_name, at cp/name-lookup.c:7027
    6 | export module hello;
      |        ^~~~~~
libbacktrace could not find executable to open
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://gcc.gnu.org/bugs/> for instructions.
In module imported at main.cc:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is 'gcm.cache/hello.gcm'
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue
compilation terminated.

如果我先编译hello.cc,那么编译器还是报错:

C:\...\hello.cc | 6 | error: failed to write compiled module: No such file or directory |
C:\...\hello.cc | 6 | note: compiled module file is 'gcm.cache/hello.gcm' |
  • 我该怎么办?
  • 我应该等待对 g++ 模块的全面支持吗?
  • 即使 g++ 部分支持,还有其他方法可以使用它吗?

【问题讨论】:

  • 我相信这是gcc.gnu.org/PR99436 并在gcc.gnu.org/g:bc56d27de97ecea813279ce5ba45b278dcccfe21 中修复。不确定 mingw 什么时候能赶上。
  • 我已经添加了std=gnu++2b 选项或std=c++2b
  • 我的意思是 gcc 模块在此评论之前不支持 c++2b。
  • ...哦...我明白了
  • 它看起来只是希望gcm.cache 目录存在,但它没有(因为它不知道创建它或没有创建它)。

标签: c++ g++ c++20 c++-modules


【解决方案1】:

我在升级现有项目时遇到了类似的问题。问题与文件扩展名有关,请确保您已将编译单元命名为hello.cpp

bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp
In module imported at main.cpp:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is ‘gcm.cache/hello.gcm’
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue
compilation terminated.
bcrowhurst@pop-os:/tmp$ mv hello.mpp hello.cpp
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp
bcrowhurst@pop-os:/tmp$ ./a.out
Hello world!

此外,我在尝试将文件重命名回hello.mpp 以查看原始错误时遇到了问题;删除 gcm.cache/ 文件夹修复它。

bcrowhurst@pop-os:/tmp$ mv hello.cpp hello.mpp
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp
/usr/bin/ld:hello.mpp: file format not recognized; treating as linker script
/usr/bin/ld:hello.mpp:1: syntax error
collect2: error: ld returned 1 exit status
bcrowhurst@pop-os:/tmp$ rm -rf gcm.cache
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp
bcrowhurst@pop-os:/tmp$ ./a.out
Hello world!

在扫描模块编译单元时,g++-11.1 似乎有一个限制(设计?)。 C++20 规范与它本身无关。

链接

https://en.cppreference.com/w/cpp/language/modules

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4720.pdf

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 2021-08-02
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多