Home >Backend Development >C++ >Why Are My C Linker Throwing 'Undefined Reference' Errors?
While configuring a C project, the linker occasionally throws puzzling errors like "Undefined reference to vtable for XXX" or "Undefined reference to ClassName::ClassName()." These errors mainly signify issues stemming from virtual functions and inheritance.
The problem often arises when a child class declares an overridden virtual function without providing a definition. Consider the following code snippet:
class Base { public: virtual void f() = 0; }; class Derived : public Base { public: void f(); };
In this example, the child class Derived declares an overridden function f() but doesn't define it. This omission triggers linker errors because while the compiler understands the declaration, the linker fails to locate the definition.
Check Library Architecture: To verify that the static libraries you're linking to are 64-bit, refer to the libraries' documentation or use the file command to inspect the library files:
file /path/to/library.a
Confirm Class Presence: To ensure the library contains the expected class and methods, use the nm command:
nm /path/to/library.a | grep SomeClass
To resolve the linker errors and complete the linking process successfully, provide the definitions for any declared virtual functions in child classes and ensure that the relevant libraries are properly included in the linking step.
The above is the detailed content of Why Are My C Linker Throwing 'Undefined Reference' Errors?. For more information, please follow other related articles on the PHP Chinese website!