Using global variables in VC++

I am using VC++, and I have problems in defining a global variable. (Please continue reading).
When I define it in a CPP there is no problem, but other file do not know it. I have tried to define it as extern but it didn’t help.
If I define the variable in a header file I get a link error (Symbol already defined in file.obj). I guess I get the error because somehow it is trying to define the variable multiple times. I have tried the following syntax to avoid it, but it didn’t solve the problem:

#if !defined(_INCLUDE_MAIN_H)
#define _INCLUDE_MAIN_H

// Define the variable

#endif

Then I define the variable as static. My problems appears to be solved!
But no!
Because the global object is static I have other problems with it… (never mind what problem).

How can I define a GLOBAL VARIABLE in a header file without using a static varibale?

Well, you would normally declare the variable in your .cpp or .c file, and then again in a common header but marked as extern.

For example:
In Main.cpp

int x;

In Main.h
extern int x;

In Someother.cpp include Main.h

But really, you should try to find a way to do without the global.

More detailed:
“static int MyVar” outside a function tells the compiler that no other source file will be able to see “MyVar”.
“extern int MyVar” tells the compiler that “MyVar” is declared in another source file as “int MyVar”, and you want to share it in the source file which the extern sentence is.

DFrey is right: try to avoid globals, and put externs in a .h. The file that declares the variable can safelly include the .h with the extern for that same variable.

thanks, I will try that.
I need this global in my program, cause I am using one Global Object who encapsulate all other “global” functions and variables.

This is not only an OpenGL question; it is
also not an “advanced” question. I suggest
you pick up Kernigan & Ritchie, or any of
the great C or C++ books published after
that, and look up “declaration” vs
“definition”.

bgl,
A. Many people ask here questions that are not OpenGL related.
B. I am not a beginner - I wrote “define” instaed of “declare” because of the language differnces. You see, I learn C++ from a book in Hebrew and I didn’t know if I should say “declare” of “define” in English. Anyway, it really doesn’t matter…
C. I agree that I should had ask this question in “begginers”.