在同一解决方案中的项目之间共享全局变量时发生链接错误 [英] link error while sharing global variables between projects in the same solution

查看:141
本文介绍了在同一解决方案中的项目之间共享全局变量时发生链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个在同一解决方案的2个项目之间共享的全局变量.

我已经在一个cpp文件中声明了
int x;
在第二个项目的另一个cpp中
extern int x;

链接错误为:
错误LNK2005:在CG_skel_w_MFC.obj

I want to make a global variable that is shared between 2 projects in the same solution.

I''ve declared in one cpp file
int x;
and in the other cpp in the second project
extern int x;

the link error was:
error LNK2005: "int x" (?x@@3HA) already defined in CG_skel_w_MFC.obj

推荐答案

中已定义"int x"(?x @@ 3HA)在公共标头中声明extern变量并将其实现为一个cpp
declare the extern variable in a common header and implement it in one cpp


联合打击战斗机飞行器C ++编码标准 [ ^ ]. "...此规则通常意味着在头文件中声明外部对象,然后将这些头文件包含在所有使用这些对象的文件中(包括定义这些对象的文件)."
Read AV Rule 139 (p.47) from Joint strike fighter air vehicle C++ coding standards[^]. "... this rule will normally mean declaring external objects in header files which will then be included in all those files that use those objects (including the files which define the objects)."


感谢您的回答,但仍然出现错误..
错误LNK2001:无法解析的外部符号
Thanks for the answer, but I still get an error..
error LNK2001: unresolved external symbol


这篇关于在同一解决方案中的项目之间共享全局变量时发生链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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