我的问题是什么时候一个函数应该引用extern关键字在C。

我看不出什么时候应该在实践中使用这种方法。当我在编写程序时,我使用的所有函数都可以通过我包含的头文件获得。那么,extern访问头文件中没有公开的内容为什么有用呢?

我可能在想extern如何不正确地工作,如果是这样,请纠正我。

也. .当它是头文件中没有关键字的默认声明时,您是否应该扩展某些内容?


当前回答

当你在不同的dll或lib上定义该函数时,编译器会根据链接器来查找它。典型的情况是当你从OS API调用函数时。

其他回答

当你在不同的dll或lib上定义该函数时,编译器会根据链接器来查找它。典型的情况是当你从OS API调用函数时。

在其他源文件中实际定义的函数应该只在头文件中声明。在这种情况下,在头文件中声明原型时应该使用extern。

大多数情况下,你的函数会是以下其中之一(更像是最佳实践):

静态(不是静态的普通函数 可见的外部。c文件) 静态内联(内联来自。c或。h 文件) 的头中的声明 下一类(见下文)) [no关键字whatever](正常 用于访问的函数 外面的声明)

如果程序中的每个文件首先被编译为一个目标文件,然后目标文件被链接在一起,则需要extern。它告诉编译器“这个函数存在,但是它的代码在其他地方。不要惊慌。”

多年以后,我发现了这个问题。看完每个回答和评论后,我想我可以澄清一些细节……这可能对通过谷歌搜索来到这里的人有用。

这个问题是专门关于使用extern函数的,所以我将忽略对全局变量使用extern。

让我们定义3个函数原型:

// --------------------------------------
// Filename: "my_project.H"
extern int function_1(void);
static int function_2(void);
       int function_3(void);

头文件可以被主要源代码使用,如下所示:

// --------------------------------------
// Filename: "my_project.C"
#include "my_project.H"

void main(void) {
    int v1 = function_1();
    int v2 = function_2();
    int v3 = function_3();
}

int function_2(void) return 1234;

为了编译和链接,我们必须在调用function_2的源代码文件中定义function_2。另外两个函数可以在不同的源代码*. c中定义,也可以位于任何二进制文件(*. c)中。OBJ, *。LIB, *. dll),我们可能没有源代码。

让我们再次将my_project.H头文件包含在不同的*. c文件中,以便更好地理解两者的区别。在同一个项目中,我们添加以下文件:

// --------------------------------------
// Filename: "my_big_project_splitted.C"
#include "my_project.H"

void old_main_test(void){
    int v1 = function_1();
    int v2 = function_2();
    int v3 = function_3();
}

int function_2(void) return 5678;

int function_1(void) return 12;

int function_3(void) return 34;

需要注意的重要特性:

When a function is defined as static in a header file, the compiler/linker must find an instance of a function with that name in each module which uses that include file. A function which is part of the C library can be replaced in only one module by redefining a prototype with static only in that module. For example, replace any call to malloc and free to add memory leak detection feature. The specifier extern is not really needed for functions. When static is not found, a function is always assumed to be extern. However, extern is not the default for variables. Normally, any header file that defines variables to be visible across many modules needs to use extern. The only exception would be if a header file is guaranteed to be included from one and only one module. Many project managers would then require that such variable be placed at the beginning of the module, not inside any header file. Some large projects, such as the video game emulator "Mame" even require that such variables appears only above the first function using them.

一篇关于extern关键字的非常好的文章,以及示例:http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

虽然我不同意在函数声明中使用extern是多余的。这应该是一个编译器设置。因此,我建议在需要时在函数声明中使用extern。