I just ran across the following error:
(.gnu.linkonce.[stuff]): undefined reference to [method] [object file]:(.gnu.linkonce.[stuff]): undefined reference to `typeinfo for [classname]’
Why might one get one of these "undefined reference to typeinfo" linker errors?
Can anyone explain what’s going on behind the scenes?
One possible reason is because you are declaring a virtual function without defining it.
When you declare it without defining it in the same compilation unit, you’re indicating that it’s defined somewhere else – this means the linker phase will try to find it in one of the other compilation units (or libraries).
An example of defining the virtual function is:
In this case, you are attaching a definition to the declaration, which means the linker doesn’t need to resolve it later.
The line
declares
fn()without defining it and will cause the error message you asked about.It’s very similar to the code:
which states that the integer
iis declared in another compilation unit which must be resolved at link time (otherwisepican’t be set to it’s address).