Hummingbird
Hummingbird

Reputation: 645

if vtable is created at compile time , why this error is an linker error and not compile error?

The following piece of code give me the error

undefined reference to `vtable for Derived'

Code :

#include <iostream>
class base{
    public:
    base(){}
    virtual ~base(){}
    virtual void test()
    {
    }
};
class Derived:public base{
    public:
    Derived(){}
    ~Derived(){}  
    void test();
};
int main() {
    base* b = new Derived ();
    delete b;
}

which i understand is because the virtual fucntion test is declared but not defined in class Derived.

But when i compile with g++ -c file.cpp which as per this Compile or assemble the source files, but do not link. It does not give me any errors and compiles fine. Hence the above error is generated at linking time and not compile time. From what i learned wasn't the vtable created at compile time. Then why do i not get the error at compile time itself?

Upvotes: 2

Views: 701

Answers (4)

Peter
Peter

Reputation: 36607

However you formed the view that a vtable must be created at compile time, you are mistaken.

Separate compilation is a core concept in the standard. It is the reason that a compilation unit (aka source file) can compile, given any declaration of a function it needs - even if it doesn't have visibility of the definition.

In the typical "compile then link" build chain, this allows a compilation unit (source file) to compile, given any declaration of a function (member function or not) that might be defined in another compilation unit.

The absence of the definition of a function then needs to be detected by the linker.

Practically, this means that the compiler may emit information about the vtable, but it will be the linker that (ahem) links the specification of the vtable to the actual member functions.

Upvotes: 2

selbie
selbie

Reputation: 104549

What I get with g++ foo.cpp -v

/tmp/ccBc4VPu.o: In function `Derived::Derived()':
foo.cpp:(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x1f): undefined reference to `vtable for Derived'
collect2: error: ld returned 1 exit status

That's a linker error, not a compiler error per se.

The root cause of the error is that test is declared in Derived, but not actually implemented. The linker is giving a confusing error message. It should be declaring an error for the missing Derived::test method

Upvotes: 2

woockashek
woockashek

Reputation: 1638

Compiler doesn't require to have all methods available. It's enough for him to have their declaration.

This method could be implemented in different compilation unit (cpp/cxx file) so for compiler it's not even possible to check if this method is available somewhere else. Compiler process one cpp file at time.

It's linker job to match methods and calls together.

Upvotes: 2

Nikita
Nikita

Reputation: 6427

GCC has an FAQ entry for this problem:

When building C++, the linker says my constructors, destructors or virtual tables are undefined, but I defined them

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.

Upvotes: 1

Related Questions