xmake icon indicating copy to clipboard operation
xmake copied to clipboard

C++模块接口重复定义时会静默成功

Open SuperSodaSea opened this issue 5 months ago • 1 comments

Xmake 版本

v2.8.7+HEAD.da279272a

操作系统版本和架构

Windows 10 专业版 2004

描述问题

当存在多个同名的 C++ 模块接口单元时,xmake 会编译成功,没有报错。

期待的结果

作为对比,CMake 会产生链接错误:

B.obj : error LNK2005: "int __cdecl f(void)" (?f@@YAHXZ::<!A>) 已经在 A.obj 中定义
ModuleTest.exe: fatal error LNK1169: 找到一个或多个多重定义的符号

个人认为 xmake 应该可以在依赖扫描阶段就发现模块重复定义,并报错。

工程配置

xmake.lua

set_languages("c++20")
target("ModuleTest")
    set_kind("binary")
    add_files("src/*.cpp", "src/*.mpp")

Main.cpp

#include <iostream>
import A;
int main() {
    std::cout << f() << std::endl;
}

A.mpp

export module A;
export int f() { return 1; }

B.mpp (与 A.mpp 内容相同)

export module A;
export int f() { return 1; }

附加信息和错误日志

除此之外还希望能够提前检测出模块分区重复定义,例如:

A.mpp

export module A;
export import :B;

B.mpp

export module A:B;
export int f();

B.cpp

module A:B;
int f() { return 1; }

实际报错:

error: A.mpp
A.mpp(3): error C7620: 无法导出模块分区 "B",它不是接口单元

可能会导致一些迷惑,所以希望这种情况也能在依赖扫描阶段识别出。


另附相关的 C++ 标准节选:

C++20 [module.unit]p2

A named module shall contain exactly one module interface unit with no module-partition, known as the primary module interface unit of the module; no diagnostic is required.

C++20 [module.unit]p3

A named module shall not contain multiple module partitions with the same module-partition.

虽然是 no diagnostic required,不过能有更友好的报错信息总是好的。

SuperSodaSea avatar Feb 26 '24 10:02 SuperSodaSea

Bot detected the issue body's language is not English, translate it automatically.


Title: C++ module interface will silently succeed when it is repeatedly defined.

Issues-translate-bot avatar Feb 26 '24 10:02 Issues-translate-bot