How to correctly use the extern keyword in C

2018-12-31 09:05发布

My question is about when a function should be referenced with the extern keyword in C.

I am failing to see when this should be used in practice. As I am writing a program all of the functions that I use are made available through the header files I have included. So why would it be useful to extern to get access to something that was not exposed in the header file?

I could be thinking about how extern works incorrectly, and if so please correct me.

Edit: Should you extern something when it is the default declaration without the keyword in a header file?

标签: c
10条回答
美炸的是我
2楼-- · 2018-12-31 09:28

extern tells the compiler that this data is defined somewhere and will be connected with the linker.

With the help of the responses here and talking to a few friends here is the practical example of a use of extern.

Example 1 - to show a pitfall:

File stdio.h:

int errno;
/* other stuff...*/

myCFile1.c:
#include <stdio.h>

Code...

myCFile2.c:
#include <stdio.h>

Code...

If myCFile1.o and myCFile2.o are linked, each of the c files have separate copies of errno. This is a problem as the same errno is supposed to be available in all linked files.

Example 2 - The fix.

File stdio.h:

extern int errno;
/* other stuff...*/

File stdio.c

int errno;

myCFile1.c:
#include <stdio.h>

Code...

myCFile2.c:
#include <stdio.h>

Code...

Now if both myCFile1.o and MyCFile2.o are linked by the linker they will both point to the same errno. Thus, solving the implementation with extern.

查看更多
若你有天会懂
3楼-- · 2018-12-31 09:28

If each file in your program is first compiled to an object file, then the object files are linked together, you need extern. It tells the compiler "This function exists, but the code for it is somewhere else. Don't panic."

查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 09:28

Functions actually defined in other source files should only be declared in headers. In this case, you should use extern when declaring the prototype in a header.

Most of the time, your functions will be one of the following (more like a best practice):

  • static (normal functions that aren't visible outside that .c file)
  • static inline (inlines from .c or .h files)
  • extern (declaration in headers of the next kind (see below))
  • [no keyword whatsoever] (normal functions meant to be accessed using extern declarations)
查看更多
墨雨无痕
5楼-- · 2018-12-31 09:34

In C, 'extern' is implied for function prototypes, as a prototype declares a function which is defined somewhere else. In other words, a function prototype has external linkage by default; using 'extern' is fine, but is redundant.

(If static linkage is required, the function must be declared as 'static' both in its prototype and function header, and these should normally both be in the same .c file).

查看更多
孤独总比滥情好
6楼-- · 2018-12-31 09:37

Many years later, I discover this question. After reading every answers and comments, I though I could clarify a few details ... This could be useful for people who get here thru goggle search.

The question is specifically about using "extern" functions, so I will ignore the use of "extern" with global variables.

Let's define 3 function prototypes

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

The header file can be used by the main source code as follow

//--------------------------------------
//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;

In order to compile and link, we must define "function_2" in the same source code file where we call that function. The two other functions could be defined in a different source code ".C" or they may be located in any binary file (.OBJ, *.LIB, *.DLL), for which we may not have the source code.

Lets include again the header "my_project.H" in a different "*.C" file to understand better the difference. In the same project, we add the following file //--------------------------------------

//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;

Important features to notice: 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 use 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 define variables to be visible across many modules need 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 manager 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 variable appear only above the first function using them.

查看更多
时光乱了年华
7楼-- · 2018-12-31 09:41

A very good article that I came about the extern keyword, along with the examples: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

Though I do not agree that using extern in function declarations is redundant. This is supposed to be a compiler setting. So I recommend using the extern in the function declarations when it is needed.

查看更多
登录 后发表回答