That should work.
Something like this:
main.h
struct test
{
int a;
};
main.cpp
#include <iostream>
using namespace std;
extern test t1;
int main(void)
{
cout << "Got: " << t1.a << endl;
return(0);
}
main2.cpp
#include "main.h"
test t1;
Now, that said, a couple of things about style.
I'm not big on global variables personally, but when they are needed, I'm much happier with implementing the Singleton pattern. This pattern will let you control *when* your variable is initialized, which is really important in the cases where you have globals that require use of one another, as well as provide a class-like interface to retrieving and utilizing the variable.
A simple singleton pattern:
class test
{
public:
static test *Get()
{
if(m_pTest == NULL)
{
m_pTest = new test;
}
return m_pTest;
}
void Shutdown()
{
if(m_pTest != NULL)
{
delete m_pTest;
}
}
int GetA() const { return m_A; }
void SetA(const int A) { m_A = A; }
private:
test() {}; // private constructor, so noone else can create
static test *m_pTest; // Static instance of this object.
int m_A;
};
Then in a CPP file you add:
test *test::m_pTest = NULL;
And you're done. :)
You use it by doing the following:
int main(void)
{
test *pTest = test::Get();
pTest->SetA(1);
pTest->GetA();
// etc.
pTest->Shutdown();
return 0;
}
Not sure if that helps or not, but I find it's very useful for larger structures that you want global. (Game-wide state information, rendering layers, etc).
Cheers!