#include "header.h"
// use obj
void bar()
{
g_obj.foo();
}
But, when possible, you should try to avoid using global variables. Pass variables through functions when feasible. This isn't always possible if you're working with low-level things like interrupts/poorly-designed callbacks, but it should be a general guideline to strive towards.
(Side-note: Think I found a bug in the formatting on the website...)
// header.h
#ifndef header_h
#define header_h
struct Foo {
int x {};
};
int func( const Foo& f );
#endif
// f0.cpp
#include "header.h"
int main(){
Foo bar {42};
int answer = func( bar );
}
// f1.cpp
#include "header.h"
int func( const Foo& f ){
return f.x;
}
They may be useful if you are creating a default state for your class (like cin and cout are objects of the iostream class), but it's worth knowing the downsides to using/providing them.
Please apply namespaces if you choose to use global variables.