jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
4.91k stars 1.77k forks source link

GCC undefined reference to 'vtable for YAML::EmitFromEvents' #1258

Closed Levi-Armstrong closed 6 months ago

Levi-Armstrong commented 6 months ago

I am trying to leverage YAML::EmitFromEvents class but was unable to compile, error undefined reference to 'vtable for YAML::EmitFromEvents' using GCC. The issue is that the class EmitFromEvents is not defining a destructor which I believe is required base on the information below. I have confirmed that adding ~EmitFromEvents() {} solves the linker issue.

GCC Frequently Asked Questions

When building C++, the linker says my constructors, destructors or virtual tables are undefined, but I defined them
The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined, but does not require any diagnostic for violations of this rule [class.virtual]/8. Based on this assumption, GCC will only emit the implicitly defined constructors, the assignment operator, the destructor and the virtual table of a class in the translation unit that defines its first such non-inline method.

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols. Unfortunately, in order to improve this error message, it might be necessary to change the linker, and this can't always be done.

The solution is to ensure that all virtual methods that are not pure are defined. Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7.
jbeder commented 6 months ago

Sounds good to me! Please submit a PR.

Also, what version of GCC? The GitHub actions already test GCC, so I'm guessing it's a reasonably old version?

Levi-Armstrong commented 6 months ago

gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)