public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lhlaurini at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104433] New: [modules] Importing <memory> and using std::make_shared causes linker errors
Date: Mon, 07 Feb 2022 20:17:08 +0000	[thread overview]
Message-ID: <bug-104433-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2022-02-07 20:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07 20:17 lhlaurini at hotmail dot com [this message]
2022-10-06 14:54 ` [Bug c++/104433] " ppalka at gcc dot gnu.org
2022-10-07 13:33 ` cvs-commit at gcc dot gnu.org
2022-10-07 13:36 ` ppalka at gcc dot gnu.org
2022-10-11 18:50 ` lhlaurini at hotmail dot com
2022-10-12  9:25 ` redi at gcc dot gnu.org
2022-10-12 19:13 ` ppalka at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104433-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).