在C中,我没有注意到在函数声明之前使用extern关键字的任何影响。 首先,我认为当定义extern int f();在单个文件中强制您在文件范围之外实现它。然而,我发现:

extern int f();
int f() {return 0;}

and

extern int f() {return 0;}

编译正常,没有来自gcc的警告。我使用gcc -Wall -ansi;它甚至不接受//注释。

在函数定义之前使用extern有什么影响吗?或者它只是一个可选的关键字,对函数没有副作用。

在后一种情况下,我不明白为什么标准设计师选择用多余的关键字乱扔语法。

编辑:为了澄清,我知道在变量中有extern的用法,但我只问函数中的extern。


当前回答

它没有效果的原因是在链接时,链接器试图解析extern定义(在您的例子中是extern int f())。它是在同一个文件中找到它还是在不同的文件中找到它并不重要,只要找到了它。

希望这能回答你的问题。

其他回答

在C语言中,函数隐式定义为extern,而不管是否实际声明关键字。

那么,代码:

    int f() {return 0;}

编译器将处理为

    extern int f() {return 0;}

从本质上讲,典型的函数定义与前面带有extern关键字的函数定义之间没有语义上的区别,如本例中所示。你可以在https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/上阅读更深入的解释

您需要区分两个独立的概念:函数定义和符号声明。"extern"是一个链接修饰符,它是对编译器的一个提示,说明后面引用的符号定义在哪里(提示是,"不在这里")。

如果我写

extern int i;

在C文件的文件范围内(函数块之外),那么你说“变量可能在其他地方定义”。

extern int f() {return 0;}

既是函数f的声明,也是函数f的定义。在这种情况下,定义覆盖了extern。

extern int f();
int f() {return 0;}

首先是声明,然后是定义。

如果您想声明和同时定义一个文件作用域变量,使用extern是错误的。例如,

extern int i = 4;

将给出一个错误或警告,这取决于编译器。

如果显式地希望避免定义变量,则使用extern非常有用。

让我解释一下:

假设文件a.c包含:

#include "a.h"

int i = 2;

int f() { i++; return i;}

文件a.h包括:

extern int i;
int f(void);

文件b.c包含:

#include <stdio.h>
#include "a.h"

int main(void){
    printf("%d\n", f());
    return 0;
}

The extern in the header is useful, because it tells the compiler during the link phase, "this is a declaration, and not a definition". If I remove the line in a.c which defines i, allocates space for it and assigns a value to it, the program should fail to compile with an undefined reference. This tells the developer that he has referred to a variable, but hasn't yet defined it. If on the other hand, I omit the "extern" keyword, and remove the int i = 2 line, the program still compiles - i will be defined with a default value of 0.

文件作用域变量是隐式定义的,如果没有显式地为它们赋值,则默认值为0或NULL——这与在函数顶部声明的块作用域变量不同。extern关键字避免了这种隐式定义,因此有助于避免错误。

对于函数,在函数声明中,关键字确实是多余的。函数声明没有隐式定义。

声明一个函数extern意味着它的定义将在链接时被解析,而不是在编译期间。

与没有声明extern的常规函数不同,它可以在任何源文件中定义(但不能在多个源文件中定义,否则会得到链接器错误,提示您已经给出了函数的多个定义),包括声明了extern的那个源文件。因此,在我们的例子中,链接器在同一个文件中解析函数定义。

我不认为这样做会很有用,但是做这样的实验可以更好地了解语言的编译器和链接器是如何工作的。

The extern keyword informs the compiler that the function or variable has external linkage - in other words, that it is visible from files other than the one in which it is defined. In this sense it has the opposite meaning to the static keyword. It is a bit weird to put extern at the time of the definition, since no other files would have visibility of the definition (or it would result in multiple definitions). Normally you put extern in a declaration at some point with external visibility (such as a header file) and put the definition elsewhere.

内联函数对于extern的含义有特殊的规则。(注意,内联函数是C99或GNU扩展;他们不是原始的C。

对于非内联函数,不需要extern,因为默认情况下它是打开的。

注意,c++的规则是不同的。例如,在要从c++调用的C函数的c++声明上需要extern“C”,关于内联有不同的规则。