C + + extern (1) extern's usefulness __c++

Source: Internet
Author: User
extern is a keyword that tells the compiler that there is a variable or function, and that if no corresponding variable or function is found, it is defined in the current file or file to see an example:
#include <iostream>
using namespace std;


int main () {

	i = 0;
	Func ();

	System ("PAUSE");
	return 0;
}


int i;
void Func () {
	i++;
	cout << "i=" << i << Endl;
}
At this point the compilation will be faulted: ' I ': Indetifier not Fonud ' func ': identifier not found
The variables I and Func in the code above are defined at the end of the file, and the compiler is defined from the top down, when Func and I cannot be identified, then the extern keyword is required to tell the compiler that the variable is in somewhere else
#include <iostream>
using namespace std;

extern int i;        use extern parameter
extern void Func ();

int main () {

	i = 0;
	Func ();

	System ("PAUSE");
	return 0;
}


int i;
void Func () {
	i++;
	cout << "i=" << i << Endl;
}

Defining functions or variables in other files, you can use the extern keyword to invoke the extern keyword, when the functions and variables are defined and declared in the. cpp or. c files.
The use example is as follows: first, we use extern to declare a variable i in the Cpp1 file.
extern int i;

Then declare the variable I and assign the value in the Cpp2 file
int i = 3;

Back to the Cpp1 file, we call the Func function output i++
#include <iostream>
using namespace std;

extern int i;
extern void Func ();

int main () {

	func ();

	System ("PAUSE");
	return 0;
}

void Func () {
	i++;
	cout << "i=" << i << Endl;
}

The results show I is 4 so after declaring the variable with the extern keyword, the compiler goes somewhere else to look for the variable very similar to the predefined function
This enables you to declare a variable or function on the other side of a CPP file to define variables or functions to declare and invoke variables in the Cpp1 file:
#include <iostream>
using namespace std;

extern int i;        DECLARE
extern void Func ();

int main () {

	func ();

	System ("PAUSE");
	return 0;
}

To define a file in the Cpp2 file:
#include <iostream>
using namespace std;

int i = 3;

void Func () {
	i++;
	cout << "i=" << i << Endl;
}



The use of the extern keyword prevents conflicting errors due to duplicate definitions: the example is as follows: Cpp1 file:
#include <iostream>
using namespace std;

int i;
extern void Func ();

int main () {

	func ();

	System ("PAUSE");
	return 0;
}

CPP2 file:
#include <iostream>
using namespace std;

int i = 3;

void Func () {
	i++;
	cout << "i=" << i << Endl;
}


An error occurred while compiling the runtime: LNK2005 "int i" (? i@@3ha) already defined in Nextfile.obj
Duplicate definition errors, at which point the I plus extern parameter can prevent duplicate definitions.
If a project is compiled CPP file, in the multiple target file link into an executable file, and two or more files, defined the same global variables, then the program will compile the error, because the compiler compiles each file separately, when the link executable file, Because multiple destination files contain the same global variables, and when the executable is generated, the global variables defined in any file are visible to the other target files, and there are errors due to variable definition conflicts

The variable or function in the header file cannot use the extern keyword because the headers are inserted into the CPP file during compilation, only the CPP file is compiled and the header file is not compiled if extern is declared in the header file, the variable or function declared by extern cannot be found in other CPP files The error occurs at this time.























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.