当摆在头文件中的静态函数定义使用C? [英] When to put static function definitions in header files in C?

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

问题描述

我已经遇到了一些code具有在头文件中的大型静态函数和我只是好奇,当它是/是不正常做到这一点。例如,如果许多 .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将AP preciated任何意见或经验法则,

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

感谢

推荐答案

一些想法:


  • 一个可能的合法使用我能想到的是,当你希望使用的功能,且不会与外部链接的符号和污染的外部空间。 (但你可以只使用一个不起眼的prefixed名称,如 mylib123__foobar 的#define foobar的mylib123__foobar 在头文件,所以这一次似乎有点玄乎。)

  • 您希望某些功能,以提供完全通过头文件,而不需要用户链接库/目标文件。我可以看到这是一个真正的动机提供了一个图书馆这是几乎没有,但数据结构和一些琐碎的碎片code来操作他们的时候。事实上,如果数据结构不是不透明的,这意味着它们可以通过应用程序直接访问,外放功能使用与他们在同一个头文件(而不是在图书馆),大大降低了摔东西的风险,如果/当你更改数据结构。

  • 或许功能仅仅是为外部函数的包装,和包装作品可能取决于在调用编译单元编译时选择的方式。例如:

  • 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?

通过话虽如此...

在现实中,人们主要的原因把静态函数在头文件通常是基于一些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%code之类的东西。 (对于这种极端情况下的一个真实世界的例子,看到一些疯狂的内联函数的/在 libav codeC 使用宏定义: - )

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天全站免登陆