Reputation: 23
Here is the code:
// A.cppm
export module AMOD;
export class A {
public:
virtual ~A() = default;
virtual foo() = 0;
};
// B.cppm
export module BMOD;
import AMOD;
export class B : public A {
public:
virtual void foo() override;
virtual ~B() = default;
};
module :private;
void B::foo() {}
// C.cppm
export module CMOD;
import BMOD;
export void bar() { B Bobj; Bobj.foo(); }
// main.cpp
import BMOD;
int main() { B b; b.foo(); }
and I got a linker error:
ld.lld: error: duplicate symbol: vtable for B@BMOD
>>> defined at main.cpp
>>> CMakeFiles/test.dir/main.cpp.o:(vtable for B@BMOD)
>>> defined at CMOD.pcm
>>> CMakeFiles/test.dir/C.cppm.o:(.data.rel.ro+0x0)
as said here https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc/Vague-Linkage.html If the class declares any non-inline, non-pure virtual functions, the first one is chosen as the “key method” for the class, and the vtable is only emitted in the translation unit where the key method is defined.
then if i make first virtual method of B inline, vtable alse will be inline (or something like that) and then error is gone. but how to properly fix it? I dont want to make it inline bc it looks like crutch
Upvotes: 1
Views: 97