g++ 错误:编译模块接口时出现“引用内部链接实体”

g++ error: "references internal linkage entity" when compiling a module interface

提问人:Kino 提问时间:11/11/2023 最后编辑:Kino 更新时间:11/11/2023 访问量:101

问:

我正在通过专业版 C++,第 5 版自学 C++,我在第 1 章中遇到了这个问题。下面是一个最小的示例:

// class.cxx
export module mod;

const int default_val { 0 };

export class Example
{
  // This line causes the problem.
  int value { default_val };
};

编译它:

$ g++ -c -std=c++20 -fmodules-ts class.cxx
class.cxx:5:14: error: ‘class Example’ references internal linkage entity ‘const int default_val’
    5 | export class Example
      |              ^~~~~~~
class.cxx:1:8: error: failed to write compiled module: Bad file data
    1 | export module mod;
      |        ^~~~~~
class.cxx:1:8: note: compiled module file is ‘gcm.cache/mod.gcm’

我试图搜索这个错误,但在互联网上没有找到它。

如果我们不在课堂上使用:default_val

export module mod;

const int default_val { 0 };

export class Example
{
  int value { 0 };
};

或者,如果文件不是模块接口:

const int default_val { 0 };

class Example
{
  int value { default_val };
};

然后它编译。

那么,是什么导致了错误以及如何解决它?提前致谢。


操作系统: Arch Linux on Kernel 6.5.9

Gcc 版本:13.2.1 20230801

C GCC G++ C++20

评论

0赞 user4581301 11/11/2023
旁注:你必须对模块有点小心。在这一点上,g++ 的模块支持是不完整的,可能还有不少 bug 仍然潜伏着,等待社区绊倒。
0赞 LHLaurini 11/11/2023
这在 Clang 中编译得很好,所以可能是一个 GCC 错误。如果你想玩模块,你可能会有更好的时间使用MSVC或Clang。
0赞 HolyBlackCat 11/11/2023
尝试。inline const int default_val { 0 };
0赞 HolyBlackCat 11/11/2023
此外,在实践中使用模块可能还为时过早。也许这本书是想面向未来,但我会再等一两年,现在使用传统的编译模式。
0赞 Kino 11/20/2023
@LHLaurini谢谢。我可以确认 Clang 编译得很好。我已将其报告为 < 的错误 gcc.gnu.org/bugzilla/show_bug.cgi?id=112621>

答: 暂无答案