https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127508

            Bug ID: 127508
           Summary: undefined reference to non-virtual thunk when using
                    modules with multiple inheritance, templates and
                    static library
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrey.antufyev at yandex dot ru
  Target Milestone: ---

The code will compile if you do any:
- Remove one (or both) of the base classes
- Make BasicObject non-template
- Move MyObject to main.cpp
- Remove MyObject entirely and use BasicObject<int> in main.cpp
- Don't use a static library and link with the object file
- Add -O1, -O2, etc.

Works on 14.4, fails on 15.1+.

Found breaking commit using git bisect:
ad30265ccfb211fca35789df2d1404cc12302219 (c++: Implement modules ABI for vtable
emissions).
Tested by reverting that commit on thunk, and it worked.

mymod.cppm
```cpp
module;
#include <iostream>
export module mymod;

export namespace mymod {
    class Writable {
    public:
        virtual ~Writable() noexcept = default;
        virtual void Write() const = 0;
    };

    class Readable {
    public:
        virtual ~Readable() noexcept = default;
        virtual void Read() = 0;
    };

    template<typename T>
    class BasicObject : public Writable, public Readable {
    public:
        virtual ~BasicObject() noexcept = default;

        void Write() const override {
            std::cout << "Write\n";
        }

        void Read() override {
            std::cout << "Read\n";
        }
    };

    class MyObject : public BasicObject<int> {
    public:
        void Test() {
            std::cout << "Hello, world!\n";
        }
    };
}
```

main.cpp
```cpp
import mymod;

int main() {
    mymod::MyObject obj;
    obj.Test();
    return 0;
}
```

g++ -std=c++20 -fmodules -o mymod.o -c mymod.cppm
ar qc libmymod.a mymod.o
g++ -std=c++20 -fmodules -o main.o -c main.cpp
g++ -o main main.o libmymod.a
```
/usr/bin/ld:
main.o:(.data.rel.ro.local._ZTVN5mymodW5mymod11BasicObjectIiEE[_ZTVN5mymodW5mymod11BasicObjectIiEE]+0x50):
undefined reference to `non-virtual thunk to
mymod::BasicObject@mymod<int>::Read()'
/usr/bin/ld:
libmymod.a(mymod.o):(.data.rel.ro.local._ZTVN5mymodW5mymod8MyObjectE[_ZTVN5mymodW5mymod8MyObjectE]+0x50):
undefined reference to `non-virtual thunk to
mymod::BasicObject@mymod<int>::Read()'
collect2: error: ld returned 1 exit status
```

g++ -v
```
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/16/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --with-gcc-major-version-only
--with-linker-hash-style=gnu --with-system-zlib --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-fixincludes --disable-libssp
--disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.1 20260810 (GCC)
```
  • [Bug c++/127508] New: undefi... andrey.antufyev at yandex dot ru via Gcc-bugs

Reply via email to