SoFunction
Updated on 2025-04-06

C++ declares the usage of extern variables and extern functions

1. Declare extern variables

1. How to declare

Add extern keyword before normal variable declaration.

2. Where to declare

Declare extern variables in header files. for example:. Do not assign values ​​when declaring variables.

3. Where to initialize

Defined in a .cpp file with the same name as the header file, for example, it must be referenced in:;

4. Where to call

Called in the main file, for example:, the main file may not have the same name as the same, but must be referenced.

The main file must not be referenced, otherwise a warning will appear: LNK2005, LNK1169;

5. Declare extern variables with examples

(1) File that declares extern variable

/*extern*/
//Declare external variablesextern int a;
extern int b;

(2) Files that initialize extern variables

/*extern*/
#include ""
 
//Define external variables and external variable initializationint a = 520;
int b = 1314;

(3) File that calls extern variable

/*extern*/
//Use external variables#include <>
#include ""
/*#include""*/ //This is not allowed, otherwise it will be warned by LNK2005 and LNK1169. 
int main(void)
{
	printf("a = %d , b = %d\n", a, b);
 
	return 0;
}
/*Run result
 a = 520 , b = 1314 */

2. Declare extern function

1. How to declare

Add extern keyword before normal function declaration.

2. Where to declare

Declare extern function in header file. For example: Declaring extern functions does not write the implementation method of function body.

3. Where to define

Define and implement the function body in a .cpp file with the same name as the header file, for example, it must be referenced in:;

4. Where to call

Called in the main file, for example:, the main file may not have the same name as the same, but must be referenced.

The main file must not be referenced, otherwise a warning will appear: LNK2005, LNK1169; for example:

Started to generate...
1>------ Started to generate: project: Project1, Configuration: Debug x64 ------
1>
1>LINK : Not found C:\Users\pc\Desktop\extern\Project1\x64\Debug\ or the previous incremental link did not generate it;Complete linking is being executed
1> : error LNK2005: "int __cdecl add(int,int)" (?add@@YAHHH@Z) Already in  Definition in
1> : error LNK2005: "int a" (?a@@3HA) Already in  Definition in
1> : error LNK2005: "int b" (?b@@3HA) Already in  Definition in
1>C:\Users\pc\Desktop\extern\Project1\x64\Debug\ : fatal error LNK1169: Find one or more multi-defined symbols
1>已完成生成project“”Operation - fail。

5. Declare extern variables with examples

(1) File that declares the extern function

/*extern*/
//Declare external variablesextern int a;
extern int b;
//Declare external functionsextern int add(int a, int b);	//externCan you please or not

(2) File that defines the extern function

/*extern*/
#include ""
 
//Define external variables and external variable initializationint a = 520;
int b = 1314;
 
//Define external functions.  Methods to implement external functionsint add(int a, int b)
{
	return a + b;
};

(3) File that calls the extern function

/*extern*/
//Use external variables#include <>
#include ""
/*#include""*/ //This is not allowed, otherwise it will be warned by LNK2005 and LNK1169. 
int main(void)
{
	printf("a = %d , b = %d\n", a, b);		//Calling external variables	printf("add(a, b) = %d\n", add(a, b));	//Calling external functions	return 0;
}
/*Run result
 a = 520 , b = 1314
 add(a, b) = 1834 */

3. Summary

  • To develop formal programming habits, variables, functions, structures, and class declarations should be placed in .h file, for example; the implementation of structures and class bodies can be placed in the file, but do not initialize. When declaring external variables, declaring external functions, etc., the extern keyword must be used.
  • Put variable initialization, function method implementation, structure implementation, and class implementation in another .cpp file. This .cpp file has the same name as the .h file by default, for example. Also, you want to reference the file. The extern keyword must be used when declaring external variables and external functions. Do not reuse the extern keyword when implementing methods such as initializing external variables, defining external functions, etc.
  • Responsible for references to variables, calls to functions, definitions of structure variables and calls to structure members, definitions of class objects and calls to class members; there is no need to use the extern keyword when calling external variables and external functions. To quote files, be sure not to quote files.

The above is the detailed content of the usage of C++ to declare extern variables and extern functions. For more information about C++ declaring extern variables and functions, please pay attention to my other related articles!