Hi all:
When I am writing C++ program, I need some global variables and structs to be visible from many files. But the compiler always complains that variable is already defined somewhere else. The problem seems to be caused by not #include file properly, but is there a good solution to avoid this irritating problem happen please
Here is an example error message:
Topology error LNK2005: "int COLUMN" ( COLUMN@@3HA) already defined in ParticleSystem.obj
Topology error LNK2005: "float UNIT" ( UNIT@@3MA) already defined in ParticleSystem.obj
Topology fatal error LNK1169: one or more multiply defined symbols found
Thanks in advance

Avoiding compiler complains about redefine variables???
jefflipe
Global variables in C++ need two things:
1. A declaration, where a variable's existence is determined.
2. A definition, where a variable's storage and initial value is determined.
First, note that when you define a variable, you are also declaring it.
Every cpp file that needs to access the global variable needs to include a file that contains the declaration. As cgraus suggested, you can place the declaration in stdafx.h (if there is one)
Since globally-scoped int x; (as an example) is a definition (initialized to zero by default), you must use extern to make it a declaration: e.g. extern int x;. Also, in direct response to the previous post extern int x = 10; is a definition since an assignment is made (extern doesn't matter.)
There can only be one definition, or you will get an linker error complaining of multiple definitions. As cgraus suggested, you can place the definition in stdafx.cpp (if there is one).
Brian
Carlos Quintero MVP
Assuming I had to create global variables, I'd declare them in stdafx.cpp, and extern them in stdafx.h. I found that worked well. In general, the extern keyword is what you want, but the stdafx thing puts them all in one place, and works well in my experience.
devbit
Nico.Kaiser
Holger,
" When I am writing C++ program, I need some global variables and structs to be visible from many files. But the compiler always complains that variable is already defined somewhere else. The problem seems to be caused by not #include file properly, but is there a good solution to avoid this irritating problem happen please "
To me this sentence is a strong hint that the OP has defined some globals in a header file ("The problem seems to be caused by not #include file properly") instead of defining them in a CU and extern them in a header, and the linker errors he gets are most likely because he #includes this header with the defined globals more than once and the header has no include guards.
I think this is more likely than that he has multiple definitions in different CUs.
So we are actually talking about the same thing - declare in a header, define in a CU.
And without include guards a lot of errors can show up, even if there are no definitions in a header file
cgraus
Since I am creating a Global variabe for any of my Classes to access. (from anywhere) I can Declare it in any of the Header file. Fine I choose one! And I Declare in it my varialble as follow:
extern int CTRL_BACKLIGHT = 0;
Then in any of the (.cpp), I should be able to access this variable, in order to change it, or anylyse it.
I am probbly not understanding something, cause this obviously does not work, I get a the weird linkage error I mentioned in the previous post.
According to my understanding of a global variable, this should be completely legal (although not much recommended ) but hey its the only option at the moment.
Thanks in advance!
mo.
That's not the problem the OP has. It's actually a linker error, which results from duplicate definitions of global variables in two different translation units.
You should declare global variables in header and define them in the .cpp files. As someone has mentioned already, you can use extern to declare a global variable:
extern int i; // tells the compiler there is a variable named "i" defined somewhere.
If you don't define the variable you'll get an unresolved external (assuming you have actually referenced i)
int i; // definition of the variable
You get the LNK2005 because in both translation units the compiler sees:
int i;
And then there's also __declspec(selectany) which tells the linker to ignore duplicate definitions (and just pick any of them, hence then name). But that's a Microsoft extension which is not available on many other C++ compilers and/or platforms.
-hg
sebapi
If you don't have an include guard the compiler will effectively see (the logical view of the translation unit after preprocessing):
int i;
//...
int i;
This will result in a compiler error (the program ill-formed).
Ok, just try the above program:
error C2086: 'int i' : redefinition
Linker errors really indicate that there are incompatible definitions in different object files.
That being said, include guards are a good thing and they should be in almost all header files. However, simply adding an include guard will not make LNK2005 errors go away. That's because they protect you from multiple definitions in a single translation unit. When you compile "file1.cpp" and include "header.h" twice, the include guard will save you. However, as soon as the second file is translated, the guard macro is again initially undefined resulting in a duplicate definition at link time.
-hg
nh1234
The error messages let me assume, that you defined your global variables in header files, which is a 'bad thing'(tm), and second, your header files seem to be without include guards.
put your globals into a cpp file and surround your header files with:
#ifndef H_SOMEHEADERNAME_INCLUDED
#define H_SOMEHEADERNAME_INCLUDED
your stuff goes here
#endif
This will prevent your headers from being included more than once
caaanerud
I have the same problem.. And it was solved by adding the "extern" keyword in from of my global variable declaration.
ei. In the header file. I have the correct
#ifndef __BlaBla_H__
#define __BlaBla_H__
then I have:
extern int CTRL_BACKLIGHT;
MY PROBLEM:
I when i try to set this integer from a fn such as
CTRL_BACKLIGHT = 23;
I get this error msg.
Linking...
CCStateMachine.obj : error LNK2001: unresolved external symbol "int CTRL_BACKLIGHT" ( CTRL_BACKLIGHT@@3HA)
radhesh
soundboy72
When used for variables, the extern keyword means that it is defined somewhere else. This means that you need at least one cpp file with that variable defined. So you should check your code and see if you have a global variable defined as
int CTRL_BACKLIGHT;
somewhere in your code.
PJFINTRAX
Or to put it nice and simply. Extern says that this variable exists somewhere in the code, but not here. So no matter how many times you use extern there must be one variable decleration without extern.
This generally goes into a cpp file because it can't be included by any other file.