这里强调一点就是关extern的声明:
extern在声明中最主要的作用就是告诉编译器别的文件引用了全局变量XXXX。
举例:
有一个工程名字叫 Project1。
Project1下面有两个.cpp源文件,分别为main.cpp和other.cpp
other.cpp内容如下:
1 char g_char ='A’;
main.cpp内容如下:
1 #include<iostream>
2 #include<Windows.h>
3
4 using namespace std;
5
6 std::string g_str;
7
8 extern char g_char; // 告诉编译器我要引用g_char这个全局变量
9 int main(void)
10 {
11 cout << "g_char===>" << g_char << endl;
12 g_char = 'B';
13 cout << "g_char===>" << g_char << endl;
14 system("pause");
15 return 0;
16 }
输出结果:
g_char===>A
g_char===>B
来源:https://www.cnblogs.com/tito/p/12358659.html