You probably really do not want to do this, but if you must - in the file that contains main:

#include "A.h"
A a;

int main() {
 ...
}

and then in the files that need to access the global:

#include "A.h" 
extern A a;

You will need to put the declaration of A in the A.h header file in order for this to work.

Answer from anon on Stack Overflow
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 47143
Global Class Objects? - C++ Forum
July 20, 2011 - Like this: Header: extern Engine engine; Cpp File: Engine engine; ... You do. extern Engine engine; is just a declaration, so you need to instantiate the engine object somewhere. ... Erm, this looks like it is inside a class or something.
Discussions

singleton - Global instance of a class in C++ - Stack Overflow
As the title says. How would I create an instance of a class that is globally available(for example I have a functor for printing and i want to have a single global instance of this(though the More on stackoverflow.com
๐ŸŒ stackoverflow.com
Can I declare class object globally in c++? - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... NOTE: I want the class object globally. More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 12, 2013
Declaring a Global Class Instance in a C++ file - Stack Overflow
I though I had a good understanding of how C++ worked but I'm confused about a piece of its use. If I declare a class instance globally in a .cpp file (not associated with a class) like Class b( More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 23, 2025
Which is better way to declare global variable in cpp
Snarky answer: neither, don't do global variables. Non-snarky: There's a third option: put it in a namespace. But given the options, the first one is "better". There's no point in declaring a class just to hold static member variables. However, there are many issues with global variables. Ranging from potential name clashes, to order of initialization issues, and potentially difficulty in testing code which accesses such global variables. More on reddit.com
๐ŸŒ r/cpp_questions
21
12
January 23, 2024
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 206333
Global Class - C++ Forum
You don't want a global instance of a window class (the sometimes academic concern about order of initialization and global objects is real with SFML.) You do want to pass a reference to the window object to functions that require it.
Top answer
1 of 3
3

It is just a static object. It is treated like any other global variable.

It will not be tied to any stack frames and will be created when the anything in the file is loaded for the first time.

Generally, people will not recommend relying on globals from a design perspective. It depends though, they can be reasonable.

If you are doing any sort of threading they can be an issue. You also want to minimize different parts of your application knowing that things are global variables, it leads to a lot of spaghetti code.

If the variable is not referenced outside of the file, or for cross-cutting concerns, then sometimes it can be a good thing.

The best advice is to avoid it when you can, don't over design it when you can't.

2 of 3
1

Objects that are global variables (or more precisely variables "at namespace scope") have static storage duration. This means they live until the end of the program, and they are initialized during program startup (either during the static or the dynamic initialization phase).

The order of initialization is not generally specified except that all such global objects are initialized before main() is called, and that the initialization does not introduce data races.

(Common techniques to sequence mutually dependent global initialization is to replace naked global variables with a global getter function and a block-static variable:

Foo & getFoo() { static Foo impl; return impl; }

Now any other global using getFoo() in its own constructor will be initialized after impl.)

Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ global-variables-in-c
Global Variables in C - GeeksforGeeks
Global variables have a program-wide scope which means can be accessed and modified by any function in the program.
Published ย  2 weeks ago
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 452289 โ€บ global-class-instance
c++ - global class instance [SOLVED] | DaniWeb
February 4, 2023 - But now I need to intantiate an instance of the class which can be accesses via that pointer, which I'm also having trouble getting my head around. ... Then I suspect you need to reorginze the logic of your program. Function foo() can't do it's job if the data doesn't exist yet. ... Indeed, I could not think of another more generic and maintainable way to do it, and my solution feels a bit hackish even if it does the job. As I said, I made a global pointer instead of global instance, then after login, created an instance and assigned reference of it to global pointer.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_storage_classes.htm
Storage Classes in C
i is 6 and count is 4 i is 7 and count is 3 i is 8 and count is 2 i is 9 and count is 1 i is 10 and count is 0 ยท The extern storage class is used to give a reference of a global variable that is visible to ALL the program files.
Top answer
1 of 5
6

So, you have to decide between a singleton and a bunch of free functions in a namespace, with some dedicated global state either way?

First, why do you want a singleton?

If it is just to ensure they share the state, consider extracting it into namespace-level TU-internal variables, and make any functions you want static. No need to muck around with singletons, which are ill regarded due to blatant over- and mis-use, aside from mis-implementation.

Next, why a class at all?

This is not Java, you can have free functions and global state outside classes, perfectly hidden from the outside unless external linkage is requested. It has the advantage of easier extensibility, and unless you need to provide a specific interface (for static injection with templates or dynamic injection with inheritance), there is no downside.

This is actually the preferred way, absent reason to do otherwise.

2 of 5
2

Assumed the free functions which belong to the same component have their own namespace, this is just a matter of taste. I am sure you won't find a hard, technical argument for or against those two approaches, they will both work and don't provide a huge difference in readability or maintainability.

So I would recommend you choose the variant which fits best to the programming style or "school of thought" of the overall system. If you prefer OO programming, use a class + single object. If you prefer hybrid style, or to use as few C++ language elements as required, use free functions. And if you don't have any preference, flip a coin.

๐ŸŒ
Google
google.github.io โ€บ styleguide โ€บ cppguide.html
Google C++ Style Guide
Every object has a storage duration, which correlates with its lifetime. Objects with static storage duration live from the point of their initialization until the end of the program. Such objects appear as variables at namespace scope ("global variables"), as static data members of classes, or as function-local variables that are declared with the static specifier.
๐ŸŒ
MIT
web.mit.edu โ€บ tibbetts โ€บ Public โ€บ inside-c โ€บ www โ€บ initializing-globals.html
The Secret Life of C++: Initializing Globals
Basically, global constructors and destructors get "registered" and called at program start and shutdown, using the _init and _fini sections of the executable.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-declare-a-global-function-in-C
How to declare a global function in C++ - Quora
A global function in C++ is simply a free function defined at namespace scope (outside any class, struct, or function).
๐ŸŒ
Quora
quora.com โ€บ How-do-I-access-a-global-variable-from-another-class
How to access a global variable from another class - Quora
Answer (1 of 2): You need extern keyword. For example, we have one global variable named a in main.cpp [code=cpp] // main.cpp int a = 1; int main() { // do something... } [/code] We want to access global variable a in MyClass.cpp, we should ...
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ how do i make a class instance global?
How do I make a class instance Global? : r/cpp_questions
August 11, 2015 - You can use the same technique in C++. Or you can use inline variables (since C++17). ... if all you need is a global that does not depend on another one and has no other globals depending on it, then this is all you need
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ general โ€บ 109265
How to create a global object/instance o - C++ Forum
If the global can be used from any function, be sure that the functions: a) Cannot be used at the same time OR b) Each function tries to lock a mutex so other functions will fail on attempt, thus not resulting in a race condition. ... Pacman is an incomplete type because you didn't use curly braces to define it. ... There are certain things you cannot do without the full definition of the class...