是否应该将函数设置为“外部”?在头文件中?

发布于 2024-09-12 01:35:59 字数 289 浏览 6 评论 0原文

是否应该在头文件中将函数设为 extern ?或者默认情况下它们是extern

例如,我应该写这个:

// birthdays.h
struct person find_birthday(const char* name);

还是这个:

// birthdays.h
extern struct person find_birthday(const char* name);

Should functions be made extern in header files? Or are they extern by default?

For example, should I write this:

// birthdays.h
struct person find_birthday(const char* name);

or this:

// birthdays.h
extern struct person find_birthday(const char* name);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

静谧 2024-09-19 01:35:59

来自C 书

如果声明包含 extern
存储类说明符,或者是
函数声明不带
存储类说明符(或两者),
那么:

  • 如果已经有该标识符的可见声明
    文件范围,生成的链接是
    与可见的相同
    声明;
  • 否则结果是外部链接。

因此,如果这是它在翻译单元中声明的唯一一次,它将具有外部链接。

From The C Book:

If a declaration contains the extern
storage class specifier, or is the
declaration of a function with no
storage class specifier (or both),
then:

  • If there is already a visible declaration of that identifier with
    file scope, the resulting linkage is
    the same as that of the visible
    declaration;
  • otherwise the result is external linkage.

So if this is the only time it's declared in the translation unit, it will have external linkage.

2024-09-19 01:35:59

标头中声明的函数通常是(除非你非常努力)extern。就我个人而言,我更喜欢在那里看到显式关键字 - 但编译器不需要它。它提醒读者他们是外部的,而且由于人类比计算机更容易犯错,我发现这个提醒很有帮助。

对于变量,在头文件中使用 extern 关键字(并且不使用初始值设定项)非常重要。因此,为了与标头中声明的(极少数)全局变量对称,我也将 extern 与该函数一起使用 - 尽管这绝对不是必要的。

Functions declared in headers are normally (unless you work really hard) extern. Personally, I prefer to see the explicit keyword there - but the compiler doesn't need it. It reminds the readers that they are extern, and since humans are more fallible than computers, I find the reminder helps.

With variables, it is important to use the extern keyword (and no initializer) in the header file. Consequently, for symmetry with the (very few) global variables declared in headers, I use extern with the function too - even though it is strictly not necessary.

暖心男生 2024-09-19 01:35:59

它们是用“extern”隐式声明的。

They are implicitly declared with "extern".

薄情伤 2024-09-19 01:35:59

不,头文件中声明的函数不需要声明 extern

但是在 .h 标头中定义的变量,然后在多个 .c 文件中定义 #included 需要声明为 extern

No, functions declared in header files do not need to be declared extern.

But variables defined in a .h header and then #included in multiple .c files will need to be declared extern.

晨曦慕雪 2024-09-19 01:35:59

我从不关心源代码中的“extern”,但有些人却这样做。在我看来,在变量之前使用 extern 而不是在函数之前使用 extern 可以使哪些东西是函数、哪些东西是变量(可能包括函数指针)更加直观。我认为很大程度上可能取决于 .h 文件中的声明是如何创建的,以及它们与主 .c 文件的关系。我通常首先输入 .h 文件原型,然后复制/粘贴到 .c 文件并添加函数体(在原型末尾敲击分号),因此必须将“extern”添加到头文件或复制/粘贴后从主 .c 文件中删除。

I never bother with the "extern" in my source code, but some people do. To my mind, having extern before variables but not functions makes it more visually obvious which things are functions and which things are variables (possibly including function pointers). I think a lot probably depends on how the declarations in the .h file are created, and how they relate to the main .c file. I usually start by typing in the .h file prototypes, and then copy/paste to the .c file and add the function body (striking the semicolon at the end of the prototype), so "extern" would require have to be added to the header file or struck from the main .c file after the copy/paste.

无言温柔 2024-09-19 01:35:59

“函数应该在头文件中设置为外部吗?还是默认情况下它们是外部的?”

应该在头文件中将函数设为 extern 吗?

  • 这是一种观点,甚至在开源中也存在差异。

我这样做的原因很简单。如果未在标头中声明,则可以在使用该函数之前在要使用它的文件中声明该函数 extern。
如果将其放入头文件中,它将包含在使用该函数的文件中。

在这方面,我认为将 extern 放在标头中更正确。
然而,编译器是故意这样做的,因为有一些糟糕的程序员不知道更多。

或者它们默认是外部的吗?

  • 函数声明默认情况下是隐式外部的。

"Should functions be made extern in header files? Or are they extern by default?"

Should functions be made extern in header files?

  • That is an opinion and differs even among open source.

The reason why I do is simple. If not declared in a header, you can declare that function extern in the file you wish to use it, right before using it.
If you put it in the header file, that will be included from the file in which the function is used.

In that respect, I think it is more correct to have the extern in the header.
However, compilers do that intentionally because there were bad programmers who didn't know any better.

Or are they extern by default?

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