Reputation: 4186
I've written a simple program in C++ using OpenGL. What I need is to have couple global variables in two files - main.cpp and funcs.cpp - which will hold the same values. When I tried to declare these variables in funcs.h (which is included also in main.cpp) I got this error:
1>main.obj : error LNK2005: "int myVariable" (?myVariable@@3HA) already defined in funcs.obj
1>Path to my program : fatal error LNK1169: one or more multiply defined symbols found
which is quite obvious, because it would create variable in funcs.cpp and main.cpp. When I tried to declare variables this way:
//funcs.cpp
int myVariable;
//main.cpp
int myVariable;
And both of them are global I'm getting exactly the same error as above. What interested me is that both variables are not visible in opposite file, so why it is wrong to have variables with the same name? As far as I know there is no possibility to reference to myVariable
in funcs.cpp from main.cpp and reverse. And my second question is - what is the best way to solve my problem, because what I did is just renamed some of these variables (there are many of them) and add functions in funcs.cpp like setNewValue(int newValue)
which I can invoke from main.cpp but to be honest - I'm not proud of that.
I use Microsoft Visual Studio 2012, C++/OpenGL.
Upvotes: 2
Views: 1398
Reputation: 59607
Using C++:
If you're trying to share the global variable between the two files, then declare it in one of them, e.g. main.c, just as you're doing now, and in the other file declare the variable as extern
. This way, the variable has been declared in one file, and the same variable can be used in other files, and you won't have problems with multiple definitions of that variable.
If you want to use a global variable with the same name in each file, such that the files don't share that variable, then use an anonymous namespace to limit the visibility of the variables:
namespace
{
int myVariable;
// ...
Standard C: For the sake of completeness, if you're using standard C, instead of using namespaces to limit the visibility of a variable, you can use the static
keyword: as above, if you want to use a global variable with the same name in each file, and you don't want the files to share that variable, then you'll need to declare them as static
. This limits their visibility to the current file.
Sharing the global using extern
works similarly in C.
Upvotes: 6
Reputation: 16578
This is not legal because it would be possible for some hypothetical third file to access myVariable
using an extern
declaration. At that point, the compiler would have no idea whether the extern
declaration referred to the variable in funcs.cpp or the one from main.cpp.
Upvotes: 0
Reputation: 179382
By default, variables at global scope are available to all other translation units; the other translation units need only use extern int myVariable
to access that global variable. So, typically, to make a global variable that's visible to every unit, you put the extern
declaration in a header file, and the variable declaration itself in exactly one .c
/.cpp
file.
If you want to make a variable accessible only in that file, declare it static
at global scope:
static int myVariable;
Upvotes: 2