首页 > 解决方案 > 无法在带有 Clang 的模块中使用对齐的“operator new”

问题描述

我正在尝试使用 Clang“模块”功能,并且正在尝试编译以下代码:

export module a;

#include <new>

export void *foo()
{
    return ::operator new(1, std::align_val_t(1));
}

export int main() {}

Try it live

当我尝试clang++ -std=c++2a -pedantic-errors -fmodules-ts --precompile -x c++-module a.cpp -o a.pcm时,我得到了

error: ISO C++ requires a definition in this translation unit for function 'operator new'
 because its type does not have linkage [-Werror,-Wundefined-internal-type]
a.cpp:7:14: note: used here
    return ::operator new(1, std::align_val_t(1));
         ^
1 error generated.

删除-pedantic-errors修复了错误,但是当我尝试使用链接生成的模块时clang++ -std=c++2a -fmodules-ts a.pcm -o a.exe,我得到

Z:\Lander\msys2\tmp\a-cfaf65.o:a.pcm:(.text+0x10): undefined reference to
 `_ZnwyW1aESt11align_val_t'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

这特别烦人,因为<iostream>(间接)似乎依赖于对齐operator new,所以我也不能在模块中使用它。以及其他一些标准标题。

这里发生了什么?

这是一个 Clang 错误,我该如何解决?


My Clang 是 MSYS2 提供的最新版本:

# clang++ --version
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-w64-windows-gnu
Thread model: posix

编辑:

提交了一个错误报告,让我们看看会发生什么......

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

解决方案


标准库不是您的模块的一部分a。所以不要在export module a;. 在此之前包含标题。


推荐阅读