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

            Bug ID: 128145
           Summary: linker failures due to multiple definitions with mingw
                    and C++ modules
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: maharmstone at gcc dot gnu.org
  Target Milestone: ---

leaf.h:

    template<typename T> int* leaf(T) { static int sv; return &sv; }
    inline int* mid() { return leaf(0); }

main.cpp:

    #include "leaf.h"
    import fe;
    int main() { return wrap() == mid(); }

mod.cpp:

    module;
    #include "leaf.h"
    export module fe;
    export inline int* wrap() { return mid(); }

other.cpp:

    #include "leaf.h"
    int* other() { return mid(); }

x86_64-w64-mingw32-g++ -std=c++20 -fmodules -O0 -c mod.cpp -o mod.o
x86_64-w64-mingw32-g++ -std=c++20 -fmodules -O0 -c main.cpp -o main.o
x86_64-w64-mingw32-g++ -std=c++20 -O0 -c other.cpp -o other.o
x86_64-w64-mingw32-g++ mod.o main.o other.o -o test.exe
/usr/libexec/gcc/x86_64-w64-mingw32/ld:
other.o:other.cpp:(.data$_ZZ4leafIiEPiT_E2sv[_ZZ4leafIiEPiT_E2sv]+0x0):
multiple definition of `leaf<int>(int)::sv'; main.o:main.cpp:(.bss+0x0): first
defined here
collect2: error: ld returned 1 exit status

I get the above linker failure with mingw on GCC 16.2.1_p20260926 on Gentoo.
Clang works, the Linux compiler works, and compiling with -O2 instead of -O0
works.

Above is a minimal reproducer, I actually encountered this while trying to
compile btrfs-dump for mingw.

Reply via email to