Computer Systems: Detailed explanation of extern attributes

What is the extern attribute?

In the C or C++ programming language, extern is a modifier used to declare a global variable or function, indicating that it is defined in other files. It allows programs to share variables or functions among different source files.

Why is the extern attribute needed?

In large programs, there are usually multiple source files, each of which can define its own global variables and functions. If you want to use a global variable or function defined in another source file in one source file, you need to use the extern attribute.

Use the extern attribute to extend the definition of a variable or function from one source file to other source files to achieve modularization and reuse of the code. This improves code readability and maintainability while reducing code redundancy.

How to use extern attribute?

for global variables

When you declare a global variable in one source file and want to use the variable in other source files, you need to use the extern keyword in other source files to reference the declaration of the variable.

For example, suppose we define a global variable x in file Ac:

// 文件A.c
int x = 10;

If you want to use the variable x in file Bc, you need to declare the variable in file Bc:

// 文件B.c
extern int x;  // 声明变量x

In this way, the variable x can be used in file Bc.

For global functions

Using the extern attribute to declare a global function is similar to declaring a global variable. In the source file that needs to call the function, use the extern keyword to reference the declaration of the function.

For example, suppose a global function foo is defined in file Ac:

// 文件A.c
void foo()
{
    
    
    // 函数体
}

If you want to call function foo in file Bc, you need to declare the function in file Bc:

// 文件B.c
extern void foo();  // 声明函数foo

In this way, the function foo can be called in the file Bc.

The difference between global variables and global functions

Although global variables and global functions are declared in the same way when using the extern attribute, they have some important differences.

storage

Global variables allocate memory space when the program is loaded, and the space is retained until the end of the program. Global functions do not occupy additional storage space, and their code is compiled directly into the executable file.

Definition and declaration

The definition of global variables includes type and initial value, and can only be done in one source file. The definition of global functions can be made in any source file and does not require an initial value.

multiple statements

Global variables can be declared multiple times but can only be defined in one source file. Global functions can be declared and defined multiple times and will be automatically resolved during connection.

Precautions

When using the extern attribute, you need to pay attention to the following points:

Consistency of declarations and definitions

The declaration of a variable or function must be consistent with its definition in different source files. If they are inconsistent, the compiler will report an error.

Multiple source files share the same variable

When multiple source files want to use the same global variable, the declaration of the variable needs to be placed in a header file and the header file is referenced in each source file. This ensures consistent variable declarations across multiple source files.

Avoid repeated inclusion of header files

When including the same header file in multiple source files, use conditional compilation to avoid repeated inclusion. A common way is to use the #ifndef and #define directives.

#ifndef _HEADER_H_
#define _HEADER_H_

// 头文件内容

#endif  // _HEADER_H_

This ensures that the same header file is only included once to avoid compilation errors.

Summarize

By using the extern attribute, we can share global variables and functions across different source files. This can improve the reusability and readability of the code, while achieving modularization of the code. When using the extern attribute, you need to pay attention to the consistency of declaration and definition, and follow some specifications and precautions.

Guess you like

Origin blog.csdn.net/m0_72410588/article/details/132893022