何时将静态函数定义放在 C 的头文件中? [英] When to put static function definitions in header files in C?

查看:28
本文介绍了何时将静态函数定义放在 C 的头文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在头文件中遇到了一些具有大型静态函数的代码,我很好奇何时可以/不可以这样做.例如,如果许多 .c 文件包含标题,为什么不定义非静态函数并将其链接到?

I've come across some code that has a large static function in a header file and i'm just curious when it is/is not ok to do this. For example, if many .c files include the header, why not just define the function non-static and link it in ?

关于何时/何时不将静态函数定义放在 C 中的头文件中的任何建议或经验法则将不胜感激,

Any advice or rules of thumb on when/when not to put static function definitions in header files in C would be appreciated,

谢谢

推荐答案

一些想法:

  • 我能想到的一种可能的合法用途是,当您希望在不创建具有外部链接的符号并污染外部命名空间的情况下使函数可用时.(但是你可以在头文件中使用一个模糊的前缀名称,如 mylib123__foobar#define foobar mylib123__foobar,所以这个看起来有点不确定.)
  • 您希望某些功能可以完全通过头文件使用,而不需要用户链接库/目标文件.当提供一个几乎没有数据结构和一些简单的代码来操作它们的库"时,我可以看到这是一个真正的动机.事实上,如果数据结构不是不透明的并且可以由应用程序直接访问,那么将与它们一起使用的函数放在同一个头文件中(而不是在库中)大大降低了更改数据时破坏事物的风险结构.
  • 也许函数只是外部函数的包装器,包装器的工作方式可能取决于调用编译单元中的编译时选项.例如:

  • One possible legitimate use I can think of is when you want to make a function available without creating a symbol with external linkage and polluting the external namespace. (But then you could just use an obscure prefixed name like mylib123__foobar, and #define foobar mylib123__foobar in the header file, so this one seems a little iffy.)
  • You want certain functionality to be available purely through the header file, without requiring the user to link a library/object files. I could see this being a real motivation when providing a 'library' that's almost nothing but data structures and a few trivial pieces of code to manipulate them. In fact if the data structures are not opaque and meant to be accessed directly by the application, putting functions for use with them in the same header file (versus in a library) greatly reduces the risk of breaking things if/when you change the data structures.
  • Perhaps the function is merely a wrapper for an external function, and the way the wrapper works might depend on compile-time options in the calling compilation unit. For example:

static int foobar(int x)
{
    return real_foobar(COMPILETIME_PARAMETER, x);
}

您可能会说只使用宏,但是如果 foobar 需要通过函数指针调用以达到预期用途怎么办?

You might say just use macros, but what if foobar needs to be called via a function pointer for the intended usage?

话虽如此……

实际上,人们将 static 函数放在头文件中的主要原因通常是基于一些 10 年过时的概念,即通过允许编译器内联函数或诸如此类来提高性能.大多数这样做的人没有做过任何测量.由于现代编译器可以在需要时将整个程序编译为一个单元,这在理论上会导致更多优化的可能性,而且由于这是一个有问题的优化开始,我真的很怀疑为了性能目的在头文件中放置函数.

In reality, the main reason people put static functions in header files is usually based on some 10-years-outdated notion that it will improve performance, by permitting the compiler to inline the function or whatnot. Most people who do this have not done any measurement. Since modern compilers can compile the whole program as a unit if asked, and this theoretically results in a lot more possibilities for optimization, and since it's a questionable optimization to begin with, I'm really skeptical of placement of functions in headers for performance purposes.

这种批评特别适用于头文件中的大"静态函数的 OP 示例.除非常量参数值允许编译器消除 90% 的代码或其他东西,否则大型函数几乎无法从内联中受益.(有关这种极端情况的真实示例,请参阅 libavcodec 中使用的一些疯狂的内联函数/宏定义.:-)

This criticism especially applies the OP's example of "large" static functions in header files. There's almost no way a large function could benefit from inlining unless a constant argument value allows the compiler to eliminate 90% of the code or something. (For a real-world example of this extreme case, see some of the crazy inline function/macro definitions used in libavcodec. :-)

这篇关于何时将静态函数定义放在 C 的头文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆