如何使用 g++ 在 c++ 20 中使用模块?

Des*_*old 6 c++ g++ c++20 c++-modules

我阅读了此链接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";
}
Run Code Online (Sandbox Code Playgroud)
// main.cc
import hello;
int main() {
  greeter("world");
  return 0;
}
// Should print "Hello world!" 
Run Code Online (Sandbox Code Playgroud)

我目前有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.
Run Code Online (Sandbox Code Playgroud)

如果我先编译hello.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.
Run Code Online (Sandbox Code Playgroud)
  • 我应该怎么办?
  • 我应该等待 g++ 中模块的完全支持吗?
  • 即使 g++ 部分支持它,还有其他方法可以使用它吗?

Ben*_*rst 3

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

\n
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp\nIn module imported at main.cpp:1:1:\nhello: error: failed to read compiled module: No such file or directory\nhello: note: compiled module file is \xe2\x80\x98gcm.cache/hello.gcm\xe2\x80\x99\nhello: note: imports must be built before being imported\nhello: fatal error: returning to the gate for a mechanical issue\ncompilation terminated.\nbcrowhurst@pop-os:/tmp$ mv hello.mpp hello.cpp\nbcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp\nbcrowhurst@pop-os:/tmp$ ./a.out\nHello world!\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
bcrowhurst@pop-os:/tmp$ mv hello.cpp hello.mpp\nbcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp\n/usr/bin/ld:hello.mpp: file format not recognized; treating as linker script\n/usr/bin/ld:hello.mpp:1: syntax error\ncollect2: error: ld returned 1 exit status\nbcrowhurst@pop-os:/tmp$ rm -rf gcm.cache\nbcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp\nbcrowhurst@pop-os:/tmp$ ./a.out\nHello world!\n
Run Code Online (Sandbox Code Playgroud)\n

看来 g++-11.1 在扫描模块编译单元时有一个限制(有意设计?)。C++20 规范本身并不关心这一点。

\n

链接

\n

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

\n

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

\n

  • 请注意,“.mpp”不在[可识别的文件扩展名](https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options)列表中。因此,在这种情况下,您需要在指定文件名之前使用“-x c++”。 (2认同)