https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104433
Bug ID: 104433 Summary: [modules] Importing <memory> and using std::make_shared causes linker errors Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lhlaurini at hotmail dot com Target Milestone: --- Greetings. I found that by importing <memory> and trying to use std::make_shared, some symbols seem to not be defined. Example: $ cat main.cpp import <memory>; int main() { std::make_shared<int>(); } $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.1.0 (GCC) $ g++ -fmodules-ts -x c++-system-header memory $ g++ -fmodules-ts main.cpp /usr/bin/ld: /tmp/ccezjsm1.o: in function `std::_Sp_make_shared_tag::_S_ti()': main.cpp:(.text._ZNSt19_Sp_make_shared_tag5_S_tiEv[_ZNSt19_Sp_make_shared_tag5_S_tiEv]+0x7): undefined reference to `std::_Sp_make_shared_tag::_S_ti()::__tag' collect2: error: ld returned 1 exit status $ g++-12 -v Using built-in specs. COLLECT_GCC=g++-12 COLLECT_LTO_WRAPPER=/home/username/.local/stow/gcc/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.0.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../gcc/configure --prefix=/home/username/.local --enable-languages=c,c++ --disable-bootstrap --disable-multilib --with-system-zlib --program-suffix=-12 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 12.0.1 20220207 (experimental) (GCC) $ g++-12 -fmodules-ts -x c++-system-header memory $ g++-12 -fmodules-ts main.cpp /usr/bin/ld: /tmp/cca9Gc2j.o: in function `std::_Sp_make_shared_tag::_S_ti()': main.cpp:(.text._ZNSt19_Sp_make_shared_tag5_S_tiEv[_ZNSt19_Sp_make_shared_tag5_S_tiEv]+0x5): undefined reference to `std::_Sp_make_shared_tag::_S_ti()::__tag' /usr/bin/ld: /tmp/cca9Gc2j.o: in function `std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count()': main.cpp:(.text._ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EED2Ev[_ZNSt14__shared_countILN9__gnu_cxx12_Lock_policyE2EED5Ev]+0x23): undefined reference to `std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release()' collect2: error: ld returned 1 exit status As you can see the code fails on both gcc 11.1 (as packaged by Arch Linux) and the latest gcc. There are more errors with the newer version, but these are caused by a change in libstdc++. >From some quick investigation, I found that the errors seem to be occur when importing a header which either: 1. has a function that returns a static constexpr variable; 2. defines a member function of a class specialization outside the class template declaration (using inline or not). Additionally, I also found that defining a member function outside the (non-template) class declaration also causes the error, unless it's marked inline, but this problem doesn't seem to be directly triggered by <memory>. This is my attempt to create some simpler test cases for showing the bug(s): $ cat header.hpp struct A { static const int& f() { static constexpr int x = 0; return x; } }; struct B { static void f1(); static void f2(); }; void B::f1() { } // This is ok inline void B::f2() { } template <typename T> struct C { static void f1(); static void f2(); }; template <> void C<int>::f1() { } template <> inline void C<int>::f2() { } $ cat main.cpp import "header.hpp"; int main() { A::f(); B::f1(); B::f2(); C<int>::f1(); C<int>::f2(); } $ g++ -fmodules-ts -x c++-header header.hpp $ g++ -fmodules-ts main.cpp /usr/bin/ld: /tmp/ccMu0ee3.o: in function `main': main.cpp:(.text+0xa): undefined reference to `B::f1()' /usr/bin/ld: main.cpp:(.text+0x14): undefined reference to `C<int>::f1()' /usr/bin/ld: main.cpp:(.text+0x19): undefined reference to `C<int>::f2()' /usr/bin/ld: /tmp/ccMu0ee3.o: in function `A::f()': main.cpp:(.text._ZN1A1fEv[_ZN1A1fEv]+0x7): undefined reference to `A::f()::x' collect2: error: ld returned 1 exit status $ g++-12 -fmodules-ts -x c++-header header.hpp $ g++-12 -fmodules-ts main.cpp /usr/bin/ld: /tmp/ccpnyIWr.o: in function `main': main.cpp:(.text+0xa): undefined reference to `B::f1()' /usr/bin/ld: main.cpp:(.text+0x14): undefined reference to `C<int>::f1()' /usr/bin/ld: main.cpp:(.text+0x19): undefined reference to `C<int>::f2()' /usr/bin/ld: /tmp/ccpnyIWr.o: in function `A::f()': main.cpp:(.text._ZN1A1fEv[_ZN1A1fEv]+0x5): undefined reference to `A::f()::x' collect2: error: ld returned 1 exit status