I'm working on a small software project which I hope to release in the future as open-source, so I was hoping to gather opinions on what the best currently accepted practices are in regards to this issue.
The application itself is procedural, not object oriented (there is no need for me to encapsulate the rendering functions or event handling functions in a class), but some aspects of the application are heavily object oriented (like the scripting console, which heavily relies on OO). The OO aspects of the code have the standard object.cpp
and object.h
files.
For the procedural part, I have my code split up into various files (e.g. main.cpp
, render.cpp
, events.cpp
), each which might have some global variables specific to that file. I also have corresponding header files for each, defining all functions and variables (as extern
) that I want to be accessible from other files. Then, I just #include
the right header when I need access to that function/variable from another source file.
I realized today that I could also have another option: create a single globals.h
header file, where I could define all global variables (as extern
again) and functions that would be needed outside of a specific source file. Then, I could just #include
this file in all of the source files (instead of each individual header file like I do now). Also, using this method, if I needed to promote a variable/function to global (instead of local), I could just add the entry to the header file.
The Question: Is it a better practice to use a corresponding header file for every single .cpp
file (and define the variables/functions I want globally accessible in those headers), or use a single header file to declare all globally accessible variables/functions?
Another quick update, most (but not all) of the globals are used as such because my application is multithreaded.