mercredi 29 juillet 2020

g++ does not link depending on optimization settings

So, this is eating me for the last two days:

I can't link my application, depending on which compiler I use and what optimizations levels are used:

  • gcc plays nicely with -O1, -O2, -O3 but fails with -O0 and
  • clang plays nicely with -O2 and -O3 and fails with -O1 and -O0.

There are a bunch of horrible templates in it, but I see no reason for this obscure behaviour.

Here is a minimal amount of code that reproduces the problem:

#include <map>
#include <memory>
#include <iostream>

using id_type = long;

class CB : public std::enable_shared_from_this<CB>
{
public:
    CB() = default;
    virtual ~CB() = default;
};

template<class F, class C> class SC : public CB
{
};

class FB
{
public:
    virtual ~FB() = default;
    template<class T, class B> T* as(B* v) const { return dynamic_cast<T*>(v);}
};

template<class T>
class F : public FB
{
public:
    virtual std::shared_ptr<CB> create() const
    {
        auto n = std::make_shared<T>();
        return n;
    }
};

struct  B
{
    virtual ~B() = default;
    static const id_type ID = 1;
};

class A : virtual public B, virtual public SC<A, B>
{
public:
    A() = default;
};

static std::map<id_type, std::shared_ptr<FB>> crtrs {
        {A::ID, std::make_shared<F<A>>()}
    };

int main()
{
    std::cout << crtrs.size();
}

Here is the same online https://gcc.godbolt.org/z/sb9b5E

And here are the error messages:

fld@flap ~/work/p/test1                                                                                                                                                                                                                                                           
> $ g++ -O1 main.cpp                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                          
> $ g++ -O2 main.cpp                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                         
> $ g++ -O3 main.cpp                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                           
> $ g++ -O4 main.cpp                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                            
> $ g++ -O0 main.cpp                                                                                                                                                                                                                                                                        
/tmp/cc8D7sNK.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1c0): undefined reference to `B::ID'
collect2: error: ld returned 1 exit status
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                            
> $ clang++ -O0 main.cpp                                                                                                                                                                                                                                                                    
/tmp/main-c49b32.o: In function `__cxx_global_var_init.1':
main.cpp:(.text.startup+0x7a): undefined reference to `B::ID'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                           
> $ clang++ -O1 main.cpp                                                                                                                                                                                                                                                                    
/tmp/main-cf18ee.o: In function `__cxx_global_var_init.1':
main.cpp:(.text.startup+0x3c): undefined reference to `B::ID'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                            
> $ clang++ -O2 main.cpp                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                             
fld@flap ~/work/p/test1                                                                                                                                                                                                                                                           
> $ clang++ -O3 main.cpp                                                                                                                                                                                                                                                                    
                                                                                     

If someone has any ideas what might be the reason, any hints are more than welcome.

Aucun commentaire:

Enregistrer un commentaire