Undefined Reference to a Static Member
In the provided code snippet, a static member variable _frequency is defined within the WindowsTimer class. However, when attempting to compile the code using a cross compiler, the following error occurs:
undefined reference to `WindowsTimer::_frequency'
This error indicates that the linker cannot locate the definition of the static member variable during linking. The reason for this is that static member variables are not automatically initialized by the compiler, and their definitions must be explicitly provided in a source file.
Solution:
To resolve this error, the definition of the static member variable _frequency needs to be provided in one of the source files of the program. This can be done by adding the following line to a .cpp file:
LARGE_INTEGER WindowsTimer::_frequency;
By providing the definition of the static member variable, the linker can successfully resolve the reference to it during linking, and the compilation error will be resolved.
It's important to note that the previous attempts to modify the definition of _frequency within the class declaration using _frequency.QuadPart = 0ull or static LARGE_INTEGER _frequency.QuadPart = 0ull would not have solved the problem, as the compiler still requires an explicit definition of the static member variable outside of the class definition.
The above is the detailed content of Why Does My Cross Compiler Produce 'undefined reference to `WindowsTimer::_frequency'' and How Do I Fix It?. For more information, please follow other related articles on the PHP Chinese website!