Linker error "vtable" (involving virtual destructor with "= default") - potential error in Clang 3.1?

I get a linker error in my code. I accurately defined it lower than lower.

This code gives the linker error "vtable for Foo" referenced: Foo :: Foo ()

class Foo { public: Foo(); virtual ~Foo() = default; }; Foo::Foo() { } 

But this code does not give any errors:

 class Foo { public: Foo(); virtual ~Foo() { } }; Foo::Foo() { } 

Why? I thought that = default was supposed to basically do the same thing as empty square brackets.

Update: I am using the "Apple LLVM compiler 4.1", part of Xcode 4.5.2. Maybe this is a bug in this compiler? Perhaps it works with the latest GCC (which Apple no longer sells). See the comments below for a discussion of compilers.

Update 2: As discussed below, changing this line to virtual inline ~Foo() = default; fixes this error. Isn't it just a mistake? It seems that the compiler does not recognize the built-in function in this case, without explicitly writing inline .

+6
source share
3 answers

This seems to be a bug in clang that has already been fixed. You asked at the right time, since a new release should appear soon: candidates for the release are already available . Try it, your example works in the i386-linux binary release and should work in all of them.

+3
source

In Itanium ABI, a v-table (and other RTTI information) is emitted for a translation block containing a definition of the first virtual method not defined by the built-in class, or if there are only virtual methods defined internally for each translation unit that includes the class. He then joins the linker to combine the redundant characters.

It is possible that by specifying = default , Clang does not understand that you have a specific virtual method built into the class, and that each TU containing your file must define v-table and RTTI, and instead expects the definition to appear where Something.

Is there a definition outside the class? => Foo::~Foo() = default;

+2
source

This works for me with g ++ 4.7.2. But I have the same problem as you have with clang 3.1.

I have 3 files.

foo.h:

 #ifndef FOO_H #define FOO_H class Foo { public: Foo(); virtual ~Foo() = default; }; #endif // FOO_H 

foo.cpp:

 #include "Foo.h" Foo::Foo() { } 

main.cpp:

 #include <iostream> #include "Foo.h" using namespace std; int main() { Foo foo; return 0; } 

But if so, it also works with clang:

Foo.cpp is empty.

main.cpp

 #include <iostream> #include "Foo.h" using namespace std; Foo::Foo() { } int main() { Foo foo; return 0; } 

So, I think clang has an error creating the object file.

+1
source

All Articles