extern
Role: .cpp
There is such a function/global variable in a file
declare external function
To a.cpp
use b.cpp
a function in , it needs to be declared with a decorated function a.cpp
before the function is used .extern
a.cpp
#include <iostream>
extern double area(int num);
int main()
{
double r;
std::cout << "Please enter radius of circle: " << std::endl;
std::cin >> r;
std::cout << area(r) << std::endl;
return 0;
}
b.cpp
#define PI 3.1415926
double area(int num) {
return PI * num * num;
}
output
Please enter radius of circle:
1
3.14159
Note: The keyword a.cpp
can be omitted in function declarations .extern
declare external global variables
To a.cpp
use b.cpp
a global variable in , it needs to be declared with a modified variable a.cpp
before the variable is used .extern
b.cpp
int a = 10;
a.cpp
#include <iostream>
extern int a;
int main()
{
std::cout << "a = " << a << std::endl;
return 0;
}
Note: In a.cpp
the declaration of global variables in , the initial value cannot be added.
If you change the code to:
extern int a = 1;
will report an error
D:\VS2019Projects\externTest\x64\Debug\externTest.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
Functions or global variables modified by extern
keywords can also be defined in their own cpp files.
E.g:main.cpp
#include <iostream>
extern int a;
extern int b;
extern int sum(int num1, int num2);
int a;
int b;
int main()
{
a = 10; b = 20;
std::cout << "sum = " << sum(a, b) << std::endl;
return 0;
}
int sum(int num1, int num2) {
return num1 + num2;
}
output
sum = 30
About redefinition
Symbols with the same name cannot be defined in multiple cpps.
- Cannot define a global variable with the same name
For example, the same global variables exist in a.cpp
and b.cpp
.
a.cpp
#include <iostream>
int a;
int main()
{
return 0;
}
b.cpp
int a;
report an error
D:\VS2019Projects\externTest\x64\Debug\externTest.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
- The same function cannot be defined. (The function is the same: the function name + parameter list is the same)
For example, the same function exists in a.cpp
and b.cpp
.
a.cpp
#include <iostream>
int sum(int num1, int num2) {
return num1 + num2;
}
int main()
{
return 0;
}
b.cpp
int sum(int num1, int num2) {
return num1 + num2;
}
report an error
D:\VS2019Projects\externTest\x64\Debug\externTest.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
C++ allows function overloading. When the function name is the same and the parameter list is different, the compiler will not report an error. E.g:
a.cpp
#include <iostream>
int sum(int num1, int num2) {
return num1 + num2;
}
int main()
{
return 0;
}
b.cpp
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
- undefined error
When cpp
a function is declared in a file, and it is called. But in the linking process did not find any definition in a cpp file. E.g:
main.cpp
Declare functions only insum
#include <iostream>
extern int sum(int num1, int num2);
int main()
{
sum(10, 20);
return 0;
}
error LNK2019: 无法解析的外部符号 "int __cdecl sum(int,int)" (?sum@@YAHHH@Z),函数 main 中引用了该符号