Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the function of the extern keyword in C++

Shulou Source: shulou.com Published: 2022-05-31 22:39:50 10月06日 Update

This article mainly explains "what is the function of the extern keyword in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of the extern keyword in C++?"

A detailed explanation of the keyword "extern" in Candlestick +

In the process of programming, variables and functions are often declared and defined, and extern comes in handy when the same global variable is shared among modules.

Define

Extern can be placed in front of a variable or function to indicate that the definition of the variable or function is in another file, prompting the compiler to look for its definition in other modules when it encounters this variable and function, without allocating memory.

Recommendation: declare in .h, because if the header file is defined, other modules include this header file, a duplicate definition error will be reported.

Experimental conclusion

1. Declare extern int gallea in .h; define int gaccouna in .c; introduce .h gaccouna in two other files is the only 2; declare int gaccouna in .h; define int gfant1 in .c; introduce .h gaccouna in two other files is the only 3. Define int g_a=1 in .h -error reporting the introduction of .h gyoga in two other files is a redefinition

Content of the experiment

There are three files: testa.h, test.c and main.c

Lab 1: declare extern int Globa in .h; define int g_a=1 in .c

Testa.h file # ifndef TESTAH#define TESTAH extern int gallea main.c main.c int main () {setA (5); printf ("g_a:%d\ n", gallea); return 0;} main.c file # include#include ".. / include/testa.h" int main () {setA (5); return 0;}

Compile: gcc testa.c main.c output: g_a:5

Lab 2: declare int Groua in .h; define int g_a=1 in .c

Just remove the extern keyword from testa.h in experiment 1

Compile: gcc testa.c main.c output: g_a:5

Lab 3: define int gallea = 1 in .h

Testa.h file

# ifndef TESTAH#define TESTAH int Gauda = 1 Battledif

Testa.c file

# include ".. / include/testa.h" void setA (int m) {return a = m;} int getA () {return gallea;}

Main.c file

# include#include ".. / include/testa.h" int main () {setA (5); printf ("g_a:%d\ n", Gauda); return 0;}

Compilation error:

/ tmp/ccja3SvL.o: (.data + 0x0): multiple definition of `g_a'/tmp/cczZlYh9.o: (.data + 0x0): first defined herecollect2: error: ld returned 1 exit status

Summary

1. The definition of variables and functions should not be defined in the header file, because when the header file is entered in # include in other files, the compiler will think that the variable has been defined twice and report an error.

2, the declaration of variables and functions in the header file (the experiment found that there is no extern keyword modification), so that other modules can use this variable and function.

In other .c or .cpp files that introduce this header file, you can also tell the compiler that it is an external reference by adding an extern variable or function declaration. It can also be used directly without a declaration.

At this point, I believe you have a deeper understanding of "what is the role of the extern keyword in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File variable function experiment key keyword compile module function two content that is compiler learning output practical deeper three global Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology macOS Shulou Information OPPO Reno