Home > Backend Development > C++ > Why Does My Cross Compiler Produce 'undefined reference to `WindowsTimer::_frequency'' and How Do I Fix It?

Why Does My Cross Compiler Produce 'undefined reference to `WindowsTimer::_frequency'' and How Do I Fix It?

Barbara Streisand
Release: 2024-12-18 21:18:12
Original
630 people have browsed it

Why Does My Cross Compiler Produce

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'
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template