在两个.cpp文件之间定义全局变量 [英] Defining Global Variables Between Two .cpp Files

查看:276
本文介绍了在两个.cpp文件之间定义全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在A.cpp和B.cpp之间共享/全局化bool变量,而这两个变量都没有包含其他.h文件? 它们具有其他联合头文件,但彼此之间没有.我可以在这些共享标头中定义全局变量吗?

How can I share/globalize a bool variable between A.cpp and B.cpp where neither of them does include the other ones .h file?? They have other joint header files but not each other's. Can I define the global variables inside those shared headers?

谢谢

推荐答案

我可以在这些共享标头中定义全局变量吗?

Can I define the global variables inside those shared headers?

否.

在A.cpp(或B.cpp)中写入

In A.cpp (or) B.cpp write,

int gVariable = 10;

请记住,仅在一个源文件中写入以上定义,否则,如果您在两个源文件中均进行写入,则链接器会抱怨多个符号.

Remember to write the above definition in only one source file or else linker will complain of multiple symbols if you write in both the source files.

在A.cpp的通用标头中,B.cpp写入,

And in the common header of A.cpp, B.cpp write,

extern int gVariable;

这篇关于在两个.cpp文件之间定义全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆