[cfe-dev] clang problem with virtual functions

Jason Sachs jmsachs at gmail.com
Sat Apr 7 09:29:07 PDT 2012


Either I'm missing something really obvious about virtual functions in
C++, or there's a bug in clang.

I have a really simple testcase here:

https://bitbucket.org/jason_s/llvm-testcases/src/fffa2dafb2f8/opt1/src

with the following main program file. Foo is a simple class with pure
virtual methods; Bar is a class that implements those.

#include <stdio.h>
#include "Bar.h"

Bar bar1, bar2;

void fibify(Foo& foo)
{
    foo.setValue(foo.fib(foo.getValue()));
}

int getfact(Foo& foo)
{
    return foo.factorial(foo.getValue());
}

int main(int argc, char **argv)
{
        bar1.setValue(5);
        bar2.setValue(5);
        bar1.setValue(bar1.fib(bar1.getValue()));
        // fibify(bar2);
        printf("bar1.getValue() = %d\n", bar1.getValue());
        printf("bar2.getValue() = %d\n", bar2.getValue());
        printf("bar1.factorial(bar1.getValue()) = %d\n",
            bar1.factorial(bar1.getValue()));
        // printf("getfact(bar1) = %d\n", getfact(bar1));
        return 0;
}

-----

The problem occurs when I uncomment the two commented lines, and I get
this in my build (Mac OSX 10.6, clang 3.1); see below.
What's going on? Did I goof up or is this a bug?

--Jason

/opt/local/libexec/llvm-3.1/bin/clang
[1/7] Foo.bc: src/Foo.cpp -> build/src/Foo.bc
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/clang -O4 -emit-llvm
-c ../src/Foo.cpp -o src/Foo.bc '
[2/7] Bar.bc: src/Bar.cpp -> build/src/Bar.bc
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/clang -O4 -emit-llvm
-c ../src/Bar.cpp -o src/Bar.bc '
[3/7] main.bc: src/main.cpp -> build/src/main.bc
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/clang -O4 -emit-llvm
-c ../src/main.cpp -o src/main.bc '
[4/7] opt1.bc: build/src/Foo.bc build/src/Bar.bc build/src/main.bc ->
build/src/opt1.bc
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/llvm-ld src/Foo.bc
src/Bar.bc src/main.bc -b src/opt1.bc '
[5/7] opt1: build/src/opt1.bc -> build/src/opt1
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/clang src/opt1.bc -o
src/opt1 '
[6/7] opt1.s: build/src/opt1.bc -> build/src/opt1.s
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/llc -O=3 src/opt1.bc
-o src/opt1.s '
[7/7] opt1.ll: build/src/opt1.bc -> build/src/opt1.ll
12:27:31 runner ' /opt/local/libexec/llvm-3.1/bin/llvm-dis src/opt1.bc
-o=src/opt1.ll '
Undefined symbols for architecture x86_64:
  "__ZdlPv", referenced from:
      __ZN3BarD0Ev in opt1-kNcANv.o
  "__ZTVN10__cxxabiv117__class_type_infoE", referenced from:
      __ZTI3Foo in opt1-kNcANv.o
  NOTE: a missing vtable usually means the first non-inline virtual
member function has no definition.
  "__ZTVN10__cxxabiv120__si_class_type_infoE", referenced from:
      __ZTI3Bar in opt1-kNcANv.o
  NOTE: a missing vtable usually means the first non-inline virtual
member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)



More information about the cfe-dev mailing list