HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

g++ undefined reference to typeinfo

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
undefinedtypeinforeference

Problem

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?

Solution

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:

virtual void fn() { /* insert code here */ }


In this case, you are attaching a definition to the declaration, which means the linker doesn't need to resolve it later.

The line

virtual void fn();


declares fn() without defining it and will cause the error message you asked about.

It's very similar to the code:

extern int i;
int *pi = &i;


which states that the integer i is declared in another compilation unit which must be resolved at link time (otherwise pi can't be set to it's address).

Code Snippets

virtual void fn() { /* insert code here */ }
virtual void fn();
extern int i;
int *pi = &i;

Context

Stack Overflow Q#307352, score: 302

Revisions (0)

No revisions yet.