Extern in C++ declares variables

  • 2020-04-02 03:01:50
  • OfStack

Extern declared variables are of the following two types:

Declare global variables
Declare functions

Today we will only talk about extern, what const, static, etc., which are related to or not related to it are ignored

Declaration and definition
Now that we're talking about extern declaring variables, we have to figure out the difference between declaration and definition.

Here we call ordinary data variables and functions variables. From a memory allocation perspective, the difference between a declaration and a definition is that declaring a variable does not allocate memory, whereas defining a variable allocates memory. A variable can be declared many times, but can only be defined once.

Based on the above premises, we can think of declarations and definitions as Pointers to memory. We know that the pointer is pointing to the memory of a symbol, the definition of a variable is just like a memory area, and the statement is its pointer, there can be multiple Pointers point to the same memory region, and a pointer can point to a memory area, so it is easy to understand why variables can be defined only once, if has been defined many times, that will be allocated more memory, so that you through variable declaration which memory area to find, this will be a problem.

For data, declarations and definitions often exist together, such as the following one-line statement


int data; 

So you declare data and you define data, so how do you declare it without defining it, using extern

extern int data ;

For functions, declarations and definitions are easy to distinguish, and we usually put declarations in the header file and definitions in the source file

void hello(); 

This is a declaration of a function, and

void hello() 

    printf("hello world!n"); 
}  

This is the definition of a function. Of course, the declaration and definition of the function can also happen at the same time, if we have no header file but only the source file, and there is no void hello() in the source file; If we want to call the function hello() in the original file, you must call the code after the function is defined.

The point here is that variables must be declared before they can be used. Declarations can be made multiple times, and definitions can be made only once. Remember this sentence and the rest will be easy to understand.

Extern declares global variables

Let's look at the following example first. There are three files: test.h, test.cpp, and main.cpp, where main.cpp and test.cpp need to share a variable g_name. The contents of the three files are as follows


 
#ifndef _TEST_H_ 
#define _TEST_H_ 
 
#include <string> 
 
std::string g_name; 
void hello(); 
 
#endif 
 
 
#include <stdio.h> 
#include "test.h" 
 
void hello() 

    printf("hello %s!n", g_name.c_str()); 

 
 
#include "test.h" 
 
std::string g_name; 
 
int main() 

    g_name = "Handy"; 
    hello(); 
    return 0; 

The relationship is that test.cpp contains test.h, and main.cpp also contains test.h. We execute the compile command


g++ main.cpp test.cpp 

Compiler error redefinition of 'g_name' says that g_name has been redefined

Where we see g_name appear, one is in the test. The h, one is in the main, CPP, two statements are STD: : string g_name, as we have said before, this way both statement also defines the variables and how is the g_name redefinition, first of all we need to understand the meaning of the include, we can understand will include a header file for all the code in the header file in the bank, as the main, CPP contains test. J h, If we expand the contents of test.h on that line, we will find two STD ::string g_name; So in main.cpp, g_name is defined twice.

Since we can think of an include header file as expanded code, we don't need to specify the header file at compile time, just the source file. It is important to note that redefinition does not mean multiple definitions in the same original file, but in the entire code space. For example, the above example refers to test.cpp and main.cpp. In fact, g_name is redefined three times in the above example, one in test.cpp and two in main.cpp.

So what's the solution to the above redefinition problem? In a nutshell, STD ::string g_name in test.h; Extern STD ::string g_name; Since the extern statement only declares variables and does not define them, test.cpp and main.cpp expand the header file and only declare g_name twice, while the real definition is still in main.cpp

Extern declares functions

Again, how do we call the hello function without the header in main.cpp? Since today's topic is extern, I don't need to remind you to use extern


 
#include <string> 
#include <stdio.h> 
 
//The statement g_name & have spent < br / > extern std::string g_name;         
 
//Declare and define void hello()& NBSP; < br / > void hello()                       

    printf("hello %s!n", g_name.c_str()); 

 
 
#include <string> 
 
//Declaration and definition g_name& NBSP; < br / > std::string g_name;    
 
//Declare void the hello () & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent < br / > extern void hello();              
 
int main() 

    g_name = "Handy" 
    hello(); 
    return 0; 

Notice that I use extern to declare variables and functions, which I comment at the end of the statement. Compile the command as follows
The same code at the page code block index 5
We didn't use header files here, but we can still share variables and functions between different files, thanks to extern!

conclusion

To understand extern, you need to understand the following concepts:

1. Distinction between declaration and definition. In the global code space, variables can have multiple declarations, but only one definition
Include header files are equivalent to expanding the code in the header files

With these two points in mind, it should be much clearer to examine the use of extern


Related articles: