是否应该将函数设置为“外部”?在头文件中?
是否应该在头文件中将函数设为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自C 书:
因此,如果这是它在翻译单元中声明的唯一一次,它将具有外部链接。
From The C Book:
So if this is the only time it's declared in the translation unit, it will have external linkage.
标头中声明的函数通常是(除非你非常努力)
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 areextern
, 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 useextern
with the function too - even though it is strictly not necessary.它们是用“extern”隐式声明的。
They are implicitly declared with "extern".
不,头文件中声明的函数不需要声明
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.我从不关心源代码中的“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.
“函数应该在头文件中设置为外部吗?还是默认情况下它们是外部的?”
应该在头文件中将函数设为 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?
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?