C语言 函数是否应该在头文件中设置为“extern”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should functions be made "extern" in header files?
提问by bodacydo
Should functions be made externin header files? Or are they externby default?
函数应该extern在头文件中创建吗?或者他们extern是默认的?
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);
采纳答案by Matthew Flaschen
From The C Book:
从C 书:
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.
如果声明包含 extern 存储类说明符,或者是没有存储类说明符(或两者)的函数声明,则:
- 如果在文件范围内已经存在该标识符的可见声明,则结果链接与可见声明的链接相同;
- 否则结果是外部链接。
So if this is the only time it's declared in the translation unit, it will have external linkage.
因此,如果这是在翻译单元中声明的唯一一次,它将具有外部链接。
回答by tur1ng
They are implicitly declared with "extern".
它们用“extern”隐式声明。
回答by Jonathan Leffler
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.
在头文件中声明的函数通常是(除非你真的很努力)extern。就个人而言,我更喜欢在那里看到显式关键字 - 但编译器不需要它。它提醒读者他们是extern,而且由于人类比计算机更容易犯错,我发现提醒有帮助。
With variables, it is important to use the externkeyword (and no initializer) in the header file. Consequently, for symmetry with the (very few) global variables declared in headers, I use externwith the function too - even though it is strictly not necessary.
对于变量,extern在头文件中使用关键字(并且没有初始值设定项)很重要。因此,为了与头文件中声明的(极少数)全局变量对称,我也使用extern该函数 - 即使它绝对不是必需的。
回答by StuartLC
No, functions declared in header files do not need to be declared extern.
不,在头文件中声明的函数不需要声明extern。
But variables defined in a .hheader and then #includedin multiple .cfiles will need to be declared extern.
但是在.h头文件和#included多个.c文件中定义的变量需要声明为 extern。
回答by supercat
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 可以在视觉上更明显地看出哪些东西是函数,哪些东西是变量(可能包括函数指针)。我认为很多可能取决于 .h 文件中的声明是如何创建的,以及它们与主 .c 文件的关系。我通常首先输入 .h 文件原型,然后复制/粘贴到 .c 文件并添加函数体(在原型末尾加上分号),因此必须将“extern”添加到头文件或复制/粘贴后从主 .c 文件中删除。

