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

            Bug ID: 125301
           Summary: [modules] [MinGW] Multiple definition linker error for
                    local static variables in exported inline functions
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mutativesystems at tutamail dot com
  Target Milestone: ---

Environment: Windows (MinGW-w64), GCC 16.1.0 (WinLibs build), flags:
`-std=c++26 -fmodules-ts`

When compiling C++20 modules on a PE/COFF target (Windows/MinGW), local static
variables inside `inline` functions that are exported from a module are not
correctly emitted as COMDAT (`__declspec(selectany)` semantics). They are
instead emitted as strong symbols. When imported and instantiated in multiple
translation units, this causes the Windows linker to fail with a "multiple
definition" error.

This issue does not occur on Linux (ELF), presumably because weak symbols
handle the merging correctly there. This bug breaks real-world libraries:
https://github.com/KhronosGroup/Vulkan-Hpp/issues/2468

To reproduce:

```
// singleton.cppm:

export module singleton;

export inline const int& get_instance() {
  static const int instance = 42;
  return instance;
}


// a.cpp:

import singleton;

const int& fetch_from_a() {
  return get_instance();
}


// main.cpp:

import singleton;

const int& fetch_from_a();

int main() {
  get_instance();
  fetch_from_a();
  return 0;
}
```

Compile with:

```
g++ -std=c++26 -fmodules-ts -c singleton.cppm -o singleton.o
g++ -std=c++26 -fmodules-ts -c a.cpp -o a.o
g++ -std=c++26 -fmodules-ts -c main.cpp -o main.o
g++ singleton.o a.o main.o -o bug_test.exe
```

This gives the error:

```
ld.exe: main.o:main.cpp:(.rdata+0x0): multiple definition of
`get_instance@singleton()::instance'; a.o:a.cpp:(.rdata+0x0): first defined
here
collect2.exe: error: ld returned 1 exit status
```
  • [Bug c++/125301] New: [mo... mutativesystems at tutamail dot com via Gcc-bugs

Reply via email to