无需在标题文件中定义函数以进行内联吗?

发布于 2025-01-22 13:47:14 字数 520 浏览 4 评论 0原文

为了使编译器内联函数调用,它需要具有完整的定义。如果未在标题文件中定义该函数,则编译器仅具有声明,即使愿意也无法内联函数。

因此,我通常会定义简短的功能,我认为编译器可能想在标题文件中内联。

进行全程图优化(/ ltcg /gl ),是否不再需要定义标头文件中的函数以允许它们被绑定?

除某些情况下,还有其他原因可以在标头文件中定义功能吗?

In order for the compiler to inline a function call, it needs to have the full definition. If the function is not defined in the header file, the compiler only has the declaration and cannot inline the function even if it wanted to.

Therefore, I usually define short functions that I imagine the compiler might want to inline in header files.

With the whole-program optimization (/LTCG and /GL), is there no longer need for defining functions in header files to allow them to be inlined?

Are there other reasons to define functions in header files, except in some cases with templates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2025-01-29 13:47:14

原因之一是因为您想分发单头库,例如 stb
同样,即使是黄金和LLD等新连接器,众所周知,链接也很慢。因此,您可能需要避免链接,而是将所有内容都包含在一个文件中。

这可以超越链接和公正的功能定义。这个想法通常是只包含一次以减少编译时间的所有内容,因为C ++汇编模型很糟糕,而且很糟糕且相当糟糕且很糟糕慢,部分原因是事情被重新编译。这是统一建立的想法。

如果您认为Unity构建听起来超级愚蠢,请考虑ubisoft(至少习惯) a>。

One reason is because you want to distribute single-header libraries, like STB.
Also, linking is notoriously slow, even with new linkers like gold and LLD. So, you might want to avoid linking, and instead include everything in a single file.

This can go beyond linking and just function definitions. The idea is to generally include everything only once to reduce compile-time, because the C++ compilation model is quite bad and slow, and part of the reason is that things are re-compiled. This is the idea of the unity build.

If you think that a unity build sounds super dumb, consider that Ubisoft (at least used to) use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文