GCC C Linker Errors: Undefined Reference to Virtual Tables and Constructors
In C , virtual functions allow derived classes to override methods defined in their base classes. When linking a program that uses virtual functions, the linker requires the definitions of these methods to be present in the object files. Failure to provide these definitions results in undefined reference errors, such as:
Possible Causes
These errors can occur for various reasons, including:
Confirming Static Libraries and Class Availability
Library Bitness: To check if static libraries are 64-bit, use the file command:
file -L <library_path>
It should show 64-bit x86-64 shared object if the library is 64-bit.
Class Availability: Use the objdump -t command to check if a library contains a specific class:
objdump -t <library_path> | grep <class_name>
If the class is present, it will be listed in the output.
Example Issue
As shown in the provided error log, the undefined reference to SomeClass::close() indicates that the class has been used but not defined. Similarly, the undefined references to SomeClass::SomeClass() and vtable for SomeOtherClass suggest that the constructor and virtual table for the SomeOtherClass class are not defined in the object files.
Solution
To resolve this issue, ensure that all virtual functions are defined in the appropriate child classes, the required header files are included, and the correct libraries are linked to your program.
The above is the detailed content of Why Am I Getting 'Undefined Reference to Virtual Tables and Constructors' Linker Errors in GCC C ?. For more information, please follow other related articles on the PHP Chinese website!