是否应该将小型实用程序函数拆分为标头和标头?源文件还是只添加内联关键字?

发布于 2025-01-12 19:33:11 字数 279 浏览 0 评论 0原文

我有一些小型实用函数(每个函数 <20 行代码),目前我将它们放入 utils.h 文件中,并在它们前面添加关键字 inline ,以便我没有遇到多个定义链接错误。这就是我一直为小型功能所做的事情。

我想知道优点和缺点是什么:

  1. 使用我的方法

  2. 将函数声明放入utils.h中并将定义放入utils.cpp

I have some small utility functions (< 20 lines of code per function) that I am currently putting in a utils.h file with the keyword inline in front of them so I don't run into multiple definition linking errors. This is what I have always done for small functions.

I am wondering what the advantages and disadvantages are to:

  1. Using my method

  2. Putting the function declarations in utils.h and definitions in utils.cpp

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

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

发布评论

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

评论(2

〗斷ホ乔殘χμё〖 2025-01-19 19:33:11

内联的缺点:

  • 如果标头包含在许多 TU 中,则由于为每个 TU 构建函数,构建时间会变慢(如果可以选择预构建标头,则构建时间可能会缩短)。
  • 当您对实现进行任何更改时,所有包含标头的 TU 都必须重新编译。如果实现经常更改(错误、新功能等)并且标头在许多 TU 中使用,则这尤其糟糕。
  • 如果实现依赖于任何定义/声明,那么这些依赖关系将转移到包含标头的所有 TU。这会增加构建这些 TU 的时间,并分散因依赖项更改而导致的重建。如果依赖关系是特定于系统的,那么系统之间的兼容性就会变得复杂。

inline的优点:

  • 如果减少TU的数量;然后从头开始构建时间就会缩短。
  • 它允许对包含它的 TU 进行内联扩展(但这也可以通过使用链接时间优化来实现)。

Disadvantage of inline:

  • If the header is included into many TU, the build time is slowed due to building of the function for each TU (this may be diminished if pre-built header is an option).
  • When you make any change to the implementation, then all TU that include the header must be re-compiled. This is especially bad if the implementation changes often (bugs, new features, etc.) and when the header is used in many TU.
  • If the implementation depends on any definition / declarations, then those dependencies transfer to all TU that include the header. This increases the time to build those TU and spreads the rebuilding caused by changes to the dependencies too. If the dependencies are system specific, then this complicates compatibility between systems.

Advantages of inline:

  • If you reduce the number of TU; then build time from scratch improves.
  • It allows inline expansion to the TU that include it (but that is also allowed by using link time optimisation).
や三分注定 2025-01-19 19:33:11

对于标头与源代码,这里的小函数存在巨大的速度差异。基本上,编译器始终内联的任何内容都是标头中的良好候选者。

内联带来的潜在收益是以编译时间为代价的。如果有一个在二进制文件中调用的实际函数,您可以在其中设置断点,这对于调试目的也很有好处。

一般来说,如果不经常调用的函数将其放在源文件中,这样会更整洁,而且没人会关心。特别是有 20 条线(不包括样板)的东西。这其实并不小。只将时间敏感的内容放入标题中。

并检查您的编译器是否可以执行 LTO(链接时间优化)。因为这样您就可以在源代码中拥有函数,并且在使用 LTO 时仍然可以将它们内联。但在开发时,您可以在没有 LTO 的情况下进行编译并获得快速构建(以及较慢的二进制文件)。因此,有了 LTO,您就可以鱼与熊掌兼得。

总的来说,我会在远低于 20 行的标题中为函数划线。如果您的函数只有 return 语句,则将其放在标头中,否则放在源中,并使用 LTO。这是我的经验法则。

For header vs. source there is a HUGE speed difference for small functions here. Basically anything that the compiler would always inline is a good candidate to be in the header.

The potential gains inlineing gives you come at the cost of compile time. It can also be nice for debugging purposes to have an actual function that gets called in the binary where you can set a breakpoint.

Generally if the function it not called often then put it in the source file, it's just neater and nobody cares. Especially something with 20 lines excluding boiler plate. That's not really small. Only put stuff in the header that is time sensitive.

And do check out if your compiler can do LTO (link time optimization). Because with that you can have functions in the source and still get them inlined when you use LTO. But while developing you can compile without LTO and get quick builds (and slower binaries). So with LTO you can have your cake and eat it too.

Overall I would draw the line for functions in headers far lower than 20 lines. If your function only has a return statement then put it in the header, otherwise the source, and use LTO. That's my rule-of-thumb.

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