Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
CPallini 15-Dec-11 18:17pm    
Please give some more details (e.g. the names of the source files using such variable). Please note: global variables should be avoided and, in any case, should have an 'avoiding conflict' name, for istance '__bull5h1_t' is good while 'x' is poor.
Chuck O'Toole 15-Dec-11 20:28pm    
both modules need to declare "x" as extern. That way, the module with the definiton "offers the variable to the global linker name space" while the other module "looks for something appearing in the global linker name space"
Albert Holguin 16-Dec-11 9:38am    
I hope you're just testing with the 'x' label on a global... that's a horrible name for a variable on the global space (although you should try to avoid globals anyway).
[no name] 16-Dec-11 23:43pm    
I think KarstenK's answer makes sense.

declare the extern variable in a common header and implement it in one cpp
 
Share this answer
 
Comments
[no name] 16-Dec-11 23:44pm    
My 5!
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)."
 
Share this answer
 
v2
Thanks for the answer, but I still get an error..
error LNK2001: unresolved external symbol
 
Share this answer
 
Comments
Albert Holguin 17-Dec-11 9:54am    
Dont post this as a solution, either update your question or reply to a specific post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900