【问题标题】:Using C++ Modules TS with standard headers on linux在 Linux 上使用带有标准头文件的 C++ 模块 TS
【发布时间】:2025-12-25 17:35:07
【问题描述】:

我正在使用模块 ts 测试一个简单的 C++ 模块:

export module strings;
#include <string>

当我尝试在 linux 上使用 clang (trunk) 和 GCC 7.2 头文件编译它时,我得到了这个:

clang++ -std=c++2a -fmodules-ts -fprebuilt-module-path=. --precompile -x c++-module -o strings.pcm ../../src/base/strings.cc
In file included from ../../src/base/strings.cc:2:
In file included from /usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/string:52:
In file included from /usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/basic_string.h:39:
In file included from /usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/ext/atomicity.h:35:
In file included from /usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/gthr.h:148:
/usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/gthr-default.h:101:1: error: weakref declaration must have internal linkage
__gthrw(pthread_once)
^
/usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/gthr-default.h:99:23: note: expanded from macro '__gthrw'
#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
                      ^
/usr/lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/gthr-default.h:90:46: note: expanded from macro '__gthrw2'
  static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
                                             ^

我尝试预先包含&lt;bits/gthr-default.h&gt;(在export module 之前),但随后我收到关于time.hsched.h 的错误...

有没有人知道编译的方法?

(有关完整的错误消息,请参见此处:https://godbolt.org/g/wfjmpW

【问题讨论】:

  • 我找到了一个 hacky 解决方案 (godbolt.org/g/vWA2aW)。只要我不需要在模块内#include &lt;ctime&gt;,它就可以工作......

标签: c++ linux c++-modules


【解决方案1】:

为了记录,我找到了一个(稍微)更好的solution

export module strings;
#if defined(__GXX_WEAK__)
#include <bits/gthr-default.h>
#endif

#include <string>

即使我需要在模块内#include &lt;ctime&gt; 这也有效。

【讨论】: