1

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

asked Aug 15, 2024 at 15:29
4
  • Are you really using precompiled headers too? Commented Aug 15, 2024 at 23:09
  • no, but don't modules work exactly the same as precompiled headers? Commented Aug 16, 2024 at 5:08
  • 1
    No: they are only generally related in purpose. Commented Aug 16, 2024 at 15:48
  • compiler issue. works only in clang trunk and gcc >= 14.1 without module :private -- godbolt.org/z/Y37xE6fG9 So waiting for compiler support Commented Aug 17, 2024 at 13:45

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.