c++inheritanceclang++unresolved-external

unresolved external to typeinfo and dynamic_cast


The problem I have is very similar to the one discussed here: g++ undefined reference to typeinfo

Even so, I believe I so not have the same issue, and the answers to that topi do not really help me. What i have is :

class Base
{
    virtual ~Base() {}
    virtual void foo() = 0;
    // some other pure virtual behaviors
};

class Derived : public Base
{
    void foo() {/* do stuff */}
    // override for all other virtual behaviors
};

then in different functions I have :

void bar( Base * base )
{
    Derived * derived = dynamic_cast<Derived *>(base);
}

void foobar( const Base & base1, const Base & base2 )
{
    if ( typeid(base1) == typeid(base2) )
        /* do something */;
}

So I'm sure the function is either pure virtual or defined (even though the object can never be Base). This should not give any problems, and it is different from the quoted discussion because i'm sure i override the virtual function. Even so, when compiling with clang++, it issues an unresolved external for both typeid and dynamic_cast when used on Derived, while it doesn't do so for other classes that inherit from Base, and override the same foo behaviors. Why does it do so?

here the errors:

error LNK2019: unresolved external symbol __imp___RTDynamicCast
error LNK2019: unresolved external symbol __imp___RTtypeid

Am i just missing something silly, or misinterpreting these errors?

Edit

I realized the code examples I first gave were not descriptive enough :

class Base
{
public:
    virtual ~Base() {}
};

class Interface : public Base
{
public:
    virtual void foo() = 0;
    // some other pure virtual behaviors
};

class Derived : public Interface
{
public:
    void foo() {/* do stuff */}
    // override for all other virtual behaviors
};

and

void bar()
{
    Base * base = new Derived;
    Interface * interface = dynamic_cast<Interface *>(base);
    interface->foo()
}

fit better to what i'm trying to do.


Solution

  • when you use dynamic_cast<Derived *>(base); in code compiler internally generate call [__imp___RTDynamicCast] instruction (this is for not x86 platform, for x86 will be call [__imp____RTDynamicCast] ). when you use typeid(base1) == typeid(base2) compiler generate call [__imp___RTtypeid] (on x86 platform will be call [__imp____RTtypeid]). when you begin link - linker view that in code used 2 symbols: __imp___RTDynamicCast and __imp___RTtypeid - he search it in all obj and lib files which you pass to him as input, but can not find. as result and give you error LNK2019: unresolved external symbol

    you need search your crt lib files - which is containing exactly this strings - __imp____RTtypeid and __imp___RTDynamicCast - because exist very many different of crt versions - impossible say, which lib will be containing this symbols. say this can be msvcurt[d].lib or vcruntime[d].lib. may be in msvcrt.lib. all what you need - add one of this libs to linker input. and he find this symbols