本文介绍了在"C"中声明的静态函数包括:头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,在源文件(即.c文件)中定义和声明静态函数是一条规则.

但是,在极少数情况下,我看到人们在头文件中声明了它.由于静态函数具有内部链接,因此我们需要在每个文件中对其进行定义,因此我们在声明该函数的头文件中包含该文件.这看起来很奇怪,而且与将某些东西声明为静态时我们通常想要的东西相去甚远.

另一方面,如果有人天真地尝试使用该函数而不定义它,则编译器会抱怨.因此,从某种意义上说,听起来有些奇怪并不是真的不安全.

我的问题是:

  • 在头文件中声明静态函数是什么问题?
  • 有哪些风险?
  • 对编译时间有什么影响?
  • 运行时是否存在任何风险?

解决方案

首先,我想澄清一下您所描述的情况:标头包含(仅)静态函数声明,而C文件包含定义,即函数的源代码.例如

some.h:

static void f();
// potentially more declarations

some.c:

#include "some.h"
static void f() { printf("Hello world\n"); }
// more code, some of it potentially using f()

如果您描述的是这种情况,我对您的评论表示怀疑

如果您声明了该函数但没有在给定的翻译单元中使用它,那么我认为您不必对其进行定义. gcc接受警告并接受它;除非我错过了一些东西,否则该标准似乎并没有禁止它.这在您的方案中可能很重要,因为不使用该功能但包含标头及其声明的转换单元不必提供未使用的定义.


现在,让我们检查一下问题:

  • 在头文件中声明静态函数是什么问题?
    这有点不寻常.通常,静态函数是仅在一个文件中需要的函数.它们被声明为静态的,以通过限制其可见性来使其明确.因此,在标头中声明它们有点相反.如果确实在具有相同定义的多个文件中使用了该函数,则应将其设为具有单个定义的外部文件.如果只有一个翻译单元实际使用它,则声明不属于标头.

    因此,一种可能的情况是为各个翻译单元中的不同实现确保统一的功能签名.通用标头会导致C(和C ++)中不同返回类型的编译时错误; 不同的参数类型仅在C语言中会导致编译时错误(但在C ++中不会由于函数重载而导致编译时间错误).
  • 有什么风险?
    我看不到您遇到的风险. (与此相反,在标头中还包含功能 definition 可能会违反封装原理.)
  • 对编译时间有什么影响?
    函数声明小且复杂度低,因此在标头中包含其他函数声明的开销可以忽略不计.但是,如果您在许多翻译单元中为声明创建并包含附加头文件,则文件处理开销可能会非常大(即,编译器在等待头文件I/O时会闲置很多)
  • 运行时是否存在任何风险?
    我看不到任何风险.

For me it's a rule to define and declare static functions inside source files, I mean .c files.

However in very rare situations I saw people declaring it in the header file.Since static functions have internal linkage we need to define it in every file we include the header file where the function is declared. This looks pretty odd and far from what we usually want when declaring something as static.

On the other hand if someone naive tries to use that function without defining it the compiler will complaint. So in some sense is not really unsafe to do this even sounding strange.

My questions are:

  • What is the problem of declaring static functions in header files?
  • What are the risks?
  • What the impact in compilation time?
  • Is there any risk in runtime?

解决方案

First I'd like to clarify my understanding of the situation you describe: The header contains (only) a static function declaration while the C file contains the definition, i.e. the function's source code. For example

some.h:

static void f();
// potentially more declarations

some.c:

#include "some.h"
static void f() { printf("Hello world\n"); }
// more code, some of it potentially using f()

If this is the situation you describe, I take issue with your remark

If you declare the function but do not use it in a given translation unit, I don't think you have to define it. gcc accepts that with a warning; the standard does not seem to forbid it, unless I missed something. This may be important in your scenario because translation units which do not use the function but include the header with its declaration don't have to provide an unused definition.


Now let's examine the questions:

  • What is the problem of declaring static functions in header files?
    It is somewhat unusual. Typically, static functions are functions needed in only one file. They are declared static to make that explicit by limiting their visibility. Declaring them in a header therefore is somewhat antithetical. If the function is indeed used in multiple files with identical definitions it should be made external, with a single definition. If only one translation unit actually uses it, the declaration does not belong in a header.

    One possible scenario therefore is to ensure a uniform function signature for different implementations in the respective translation units. The common header leads to a compile time error for different return types in C (and C++); different parameter types would cause a compile time error only in C (but not in C++' because of function overloading).
  • What are the risks?
    I do not see risks in your scenario. (As opposed to also including the function definition in a header which may violate the encapsulation principle.)
  • What the impact in compilation time?
    A function declaration is small and its complexity is low, so the overhead of having additional function declarations in a header is likely negligible. But if you create and include an additional header for the declaration in many translation units the file handling overhead can be significant (i.e. the compiler idles a lot while it waits for the header I/O)
  • Is there any risk in runtime?
    I cannot see any.

这篇关于在"C"中声明的静态函数包括:头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 20:57
查看更多