有没有办法在 C 中的同一个头文件中拥有静态原型和公共原型?

发布于 2024-12-12 04:32:31 字数 574 浏览 0 评论 0原文

当我在头文件中混合 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 技术交流群。

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

发布评论

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

评论(4

眼泪也成诗 2024-12-19 04:32:31

不要将静态函数的声明放在头文件中。由于它们对于定义它们的 .c 文件来说是本地的,因此从头文件中导出它们是没有意义的。

您可以做两件事:

  1. 在 c 文件中调用函数之前定义该函数(将 a() 的定义移至 main 上方)。
  2. 在 C 文件顶部声明静态函数(这是我个人的选择)。在您的例子中,您将 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:

  1. Define the function before it is called in the c file (move definition of a() above main).
  2. Declare static function at the top of the C file (this is my personal choice). In your case, you move the declaration of a() to the top of main.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.

素年丶 2024-12-19 04:32:31

在标头中包含静态原型是没有意义的,原因是静态函数具有文件作用域,因此外部模块无论如何都无法访问这些函数。

相反,我建议您仅将静态原型放在定义它们的同一文件中文件顶部的 .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.

遇见了你 2024-12-19 04:32:31

如果您在 main.h 中有 b() 的声明,为什么在包含它的 other.c 中再次需要它呢?除此之外,一切都很好,看不出任何问题。

关于“普通”函数要记住的重要一点是,同一函数的所有前向声明(在本例中为 b() )必须匹配,否则您将遇到麻烦(链接错误,编译器错误,什么不是)。在你的情况下,它们不匹配。

当然,除非 b(); 是对该函数的实际调用,但至少在您将其发布到任何范围之外的代码中,因此被视为前向声明。

对于静态前向声明,它们限制了函数对编译单元的可见性。因此,在 other.c 中调用 a() 不会执行 main 中实现的 a() .c。这就是 C 中static 的全部意义。

If you have the declaration of b() in main.h, why do you need it again in other.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 calling a() in other.c will not execute the a() implemented in main.c. That's the whole point of static in C.

迷路的信 2024-12-19 04:32:31

如果您要定义的“静态”函数足够短或足够便宜,您最好将其定义为静态内联并将其定义及其主体(不仅仅是其声明)放在 *< 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.

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