有没有办法在 C 中的同一个头文件中拥有静态原型和公共原型?
当我在头文件中混合 public 和 static 原型时,编译器总是会抱怨。
假设我有一个 header main.h,其中包含函数 a 的静态原型和函数 b 的普通原型。然后我想#include
两个.c文件中的头文件,即main.c和other.c。在 main.c 中,我需要一个包含,因为我在任何原型之前调用该函数。
main.h:
static int a(void);
int b(void);
main.c:
#include "main.h"
void main(){
a();
}
static int a(void){
/* do stuff */
}
int b(void){
/* do stuff */
}
other.c:
#include "main.h"
b();
除了将头文件分割成一个单独的头文件专门用于静态原型和一个用于公共原型的明显解决方案之外,最佳实践是什么?
The compiler will always whine when I have mixed public with static prototypes in a header file.
Say I have header main.h with a static prototype for function a and a normal prototype for function b. Then I want to #include
the header in two .c files, namely main.c and other.c. In main.c I need an include as I call the function before any prototype.
main.h:
static int a(void);
int b(void);
main.c:
#include "main.h"
void main(){
a();
}
static int a(void){
/* do stuff */
}
int b(void){
/* do stuff */
}
other.c:
#include "main.h"
b();
What's the best practice for this than the obvious solution of splitting the header file into a seperate header exclusively for static prototypes and one for public ones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要将静态函数的声明放在头文件中。由于它们对于定义它们的
.c
文件来说是本地的,因此从头文件中导出它们是没有意义的。您可以做两件事:
a()
的定义移至 main 上方)。a()
的声明移动到main.c
的顶部。如果该函数要在多个翻译单元中使用,那么您可以在头文件中定义静态函数。假设您有一个支持 C99 的成熟编译器,它可以有效地内联而不是静态。
You don't put declarations of static functions in header files. Since they are local to the
.c
file that defines them, it doesn't make sense to export them from a header file.There are two things you can do:
a()
above main).a()
to the top ofmain.c
.If the function is going to be used in multiple translation units, then you can define the static function in the header file. It could usefully be inline instead of static assuming you have a grown-up compiler that supports C99.
在标头中包含静态原型是没有意义的,原因是静态函数具有文件作用域,因此外部模块无论如何都无法访问这些函数。
相反,我建议您仅将静态原型放在定义它们的同一文件中文件顶部的 .c 文件中。
It makes no sense having static prototypes in a header, the reason for this is that static functions have file scope so no module outside can anyway access the functions.
Instead I would suggest you put static prototypes only in the .c file at the top of the file in the same file they are defined.
如果您在
main.h
中有b()
的声明,为什么在包含它的other.c
中再次需要它呢?除此之外,一切都很好,看不出任何问题。关于“普通”函数要记住的重要一点是,同一函数的所有前向声明(在本例中为
b()
)必须匹配,否则您将遇到麻烦(链接错误,编译器错误,什么不是)。在你的情况下,它们不匹配。当然,除非
b();
是对该函数的实际调用,但至少在您将其发布到任何范围之外的代码中,因此被视为前向声明。对于静态前向声明,它们限制了函数对编译单元的可见性。因此,在
other.c
中调用a()
不会执行main 中实现的
。这就是 C 中a()
.cstatic
的全部意义。If you have the declaration of
b()
inmain.h
, why do you need it again inother.c
that includes it? Other than that, it's all good, can't see any issue.The important point to keep in mind about the "normal" functions is that all the forward declarations of the same function (
b()
in this case) must match, otherwise you'll have trouble (linking errors, compiler errors, what's not). In your case they don't match.Unless of course
b();
is an actual call to the function, but at least in the code you posted its out of any scope, thus is treated as a forward declaration.Re the
static
forward declarations, they limit the visibility of the function to the compilation unit. So callinga()
inother.c
will not execute thea()
implemented inmain.c
. That's the whole point ofstatic
in C.如果您要定义的“静态”函数足够短或足够便宜,您最好将其定义为静态内联并将其定义及其主体(不仅仅是其声明)放在 *< em>.h 头文件。
IF the "static" function you want to define is short enough or cheap enough, you'll better define it as static inline and put its definition with its body (not only its declaration) in the *.h header file.