简体   繁体   English

C++ 模块的两步编译?

[英]Two-Step Compile for C++ Modules?

Clang and GCC (and maybe MSVC?) are using a two-step compile for their modules implementation at the moment: Clang 和 GCC(也许还有 MSVC?)目前正在使用两步编译来实现他们的模块:

  • Generate the BMI/CMI (IPR for MSVC, if it still does this?) to be consumed by someone else's import.生成 BMI/CMI(MSVC 的 IPR,如果它仍然这样做?)以供其他人的导入使用。
  • Generate the object file to be feed to the linker.生成 object 文件以馈送到 linker。

It seems that there are some possible uses for modules that produce a BMI/CMI but that don't produce an object file, for example modules that only export types or constexpr variables used for conditional compilation.生成 BMI/CMI 但不生成 object 文件的模块似乎有一些可能的用途,例如仅导出用于条件编译的类型或 constexpr 变量的模块。

As far as I can understand from the standard, there's nothing saying I have to produce/link the objects file.据我从标准中可以理解,没有什么说我必须生成/链接对象文件。 So I'm wondering if I've missed something obvious about using modules like this, and if we expect tooling to support this "build as a module, don't build as an object" workflow?所以我想知道我是否错过了关于使用这样的模块的一些明显的东西,如果我们希望工具支持这种“作为模块构建,而不是作为对象构建”的工作流程?

I expect modules to be able to provide definition for stuff that normally wouldn't be with headers.我希望模块能够为通常不会包含标题的内容提供定义。

Imagine this module:想象一下这个模块:

export module hello;

export inline auto say_hello() -> char const* {
    return "hello world";
}

As you can see, the function is inline.如您所见,function 是内联的。 It is also in the interface.它也在界面中。 Now with header, there is no place to put the implementation.现在有了 header,没有地方放实现了。 For inline function to be possible, the language allow for multiple definition to be found.为了使内联 function 成为可能,该语言允许找到多个定义。 So each TU output their own definition in the object file.所以每个TU output自己在object文件中定义。

That is repeated work that can be avoided using module.这是使用模块可以避免的重复工作。 As you can see, module interface are TU just like any other cpp file.如您所见,模块接口就像任何其他 cpp 文件一样是 TU。 When you export an inline function, yes the implementation is available to other TU, but it won't be necessary for all tu to provide implementation, since it can be put in one place: the TU that has the inline function in it.当您导出内联 function 时,是的,该实现可用于其他 TU,但并非所有tu 都必须提供实现,因为它可以放在一个地方:具有内联 function 的 TU。

I expect the same thing with constexpr variables.我期望与 constexpr 变量相同。 They also need definition, since you may take a reference or an address to them.它们也需要定义,因为您可以为它们提供参考或地址。 Take this for example:以此为例:

export module foo;
import <tuple>;
export constexpr auto tup = std::tuple{1, 'a', 5.6f};
import foo;
int a = std::get<0>(tup);

The std::get function takes a reference to the tuple. std::get function 引用元组。 Even though it's a constexpr variable, some context (especially without optimizations) may require the variable to be ODR used.即使它是一个 constexpr 变量,某些上下文(尤其是没有优化的情况)可能需要使用 ODR 变量。

So in my example, even though the module foo only export a constexpr variable, I expect the cpp file to compile into an object file containing the definition.因此,在我的示例中,即使模块foo仅导出 constexpr 变量,我希望 cpp 文件编译为包含定义的 object 文件。


It may also happen that there is nothing inside the object file. object 文件中也可能没有任何内容。 I also expect it to behave like an empty TU today:我也希望它今天表现得像一个空的 TU:

// I'm empty

You can add such cpp file into a project without any problem, and link it to your executable.您可以毫无问题地将此类cpp文件添加到项目中,并将其链接到您的可执行文件。 I expect the tools to behave the same with modules.我希望这些工具与模块的行为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM