定义了全局变量,在不同的cpp文件中都要extern吗?
程序内容如下:
全局变量在global.h,有两个大类test1和test2,定义和实现函数分别为:
test1.h、test1.cpp 和 test1.h、test2.cpp
其中test1.cpp 和 test2.cpp 都用到全局变量;并且 test1.cpp调用test2类
问题:
1、我在test1.cpp中extern全局变量,编译时会出现test2.cpp中的全局变量没有定义
2、如果在test1.cpp 和 test2.cpp 中都extern全局变量的话,编译时会出现 在test2.cpp中的全局变量已经定义
问题该怎么解决呀?
谢谢!
[解决办法]
1 使用extern