How to fix ROOT error: ‘gROOT’ was not declared in this scope

Problem:

While compiling a ROOT-based C++ program, you get an error message like this:

main.cpp: In member function ‘int main()’:
main.cpp:26:22: error: ‘gROOT’ was not declared in this scope; did you mean ‘ROOT’?
   26 |         auto cdpad = gROOT->GetSelectedPad();

Solution:

The error message is a bit misleading. The problem is that the gROOT object is not available in the global scope. You need to include the ROOT header file TROOT.h to get access to it. Include the following line at the top of your source file:

#include <TROOT.h>