typeinfo 和 dynamic_cast 外部未解决
unresolved external to typeinfo and dynamic_cast
我遇到的问题与此处讨论的问题非常相似:
g++ undefined reference to typeinfo
即便如此,我相信我不会遇到同样的问题,而且那个话题的答案对我没有真正的帮助。
我拥有的是:
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
};
然后在不同的函数中我有:
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 */;
}
所以我确定该函数是纯虚拟的或已定义的(即使该对象永远不可能是 Base)。这应该不会有任何问题,并且与引用的不同
讨论,因为我确定我重写了虚函数。
即便如此,当使用 clang++ 编译时,它会在 Derived 上使用时为 typeid 和 dynamic_cast 发出未解析的外部,而对于从 Base 继承的其他 类 则不会这样做,并覆盖相同的foo 行为。
为什么要这样做?
此处错误:
error LNK2019: unresolved external symbol __imp___RTDynamicCast
error LNK2019: unresolved external symbol __imp___RTtypeid
我是不是漏掉了一些愚蠢的东西,或者误解了这些错误?
编辑
我意识到我最初给出的代码示例描述性不够:
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
};
和
void bar()
{
Base * base = new Derived;
Interface * interface = dynamic_cast<Interface *>(base);
interface->foo()
}
更适合我正在尝试做的事情。
当您在代码编译器中使用 dynamic_cast<Derived *>(base);
时,会在内部生成 call [__imp___RTDynamicCast]
指令(这不是针对 x86 平台,对于 x86 将调用 [__imp____RTDynamicCast]
)。当您使用 typeid(base1) == typeid(base2)
编译器时生成 call [__imp___RTtypeid]
(在 x86 平台上将是 call [__imp____RTtypeid]
)。当你开始 link - linker 认为在代码中使用了 2 个符号:__imp___RTDynamicCast
和 __imp___RTtypeid
- 他在你传递给他的所有 obj 和 lib 文件中搜索它作为输入,但找不到。结果并给你错误 LNK2019: unresolved external symbol
你需要搜索你的 crt 库文件——它恰好包含这个字符串——__imp____RTtypeid
和 __imp___RTDynamicCast
——因为存在很多不同的 crt 版本——不可能说,哪个库将包含这个符号。假设这可以是 msvcurt[d].lib
或 vcruntime[d].lib
。可能在 msvcrt.lib
。所有你需要的 - 添加这个库之一到 linker 输入。他找到了这个符号
我在 Visual studio 和 CodeBlocks 上使用 Clang 5.0 平台工具集 LLVM-VS2014 时遇到了这个问题。
我添加了 msvcrt.lib 以提供缺少的 __imp____RTDynamicCast。
这个应该怎么推导出来,我还没看懂:-(
我遇到的问题与此处讨论的问题非常相似: g++ undefined reference to typeinfo
即便如此,我相信我不会遇到同样的问题,而且那个话题的答案对我没有真正的帮助。 我拥有的是:
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
};
然后在不同的函数中我有:
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 */;
}
所以我确定该函数是纯虚拟的或已定义的(即使该对象永远不可能是 Base)。这应该不会有任何问题,并且与引用的不同 讨论,因为我确定我重写了虚函数。 即便如此,当使用 clang++ 编译时,它会在 Derived 上使用时为 typeid 和 dynamic_cast 发出未解析的外部,而对于从 Base 继承的其他 类 则不会这样做,并覆盖相同的foo 行为。 为什么要这样做?
此处错误:
error LNK2019: unresolved external symbol __imp___RTDynamicCast
error LNK2019: unresolved external symbol __imp___RTtypeid
我是不是漏掉了一些愚蠢的东西,或者误解了这些错误?
编辑
我意识到我最初给出的代码示例描述性不够:
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
};
和
void bar()
{
Base * base = new Derived;
Interface * interface = dynamic_cast<Interface *>(base);
interface->foo()
}
更适合我正在尝试做的事情。
当您在代码编译器中使用 dynamic_cast<Derived *>(base);
时,会在内部生成 call [__imp___RTDynamicCast]
指令(这不是针对 x86 平台,对于 x86 将调用 [__imp____RTDynamicCast]
)。当您使用 typeid(base1) == typeid(base2)
编译器时生成 call [__imp___RTtypeid]
(在 x86 平台上将是 call [__imp____RTtypeid]
)。当你开始 link - linker 认为在代码中使用了 2 个符号:__imp___RTDynamicCast
和 __imp___RTtypeid
- 他在你传递给他的所有 obj 和 lib 文件中搜索它作为输入,但找不到。结果并给你错误 LNK2019: unresolved external symbol
你需要搜索你的 crt 库文件——它恰好包含这个字符串——__imp____RTtypeid
和 __imp___RTDynamicCast
——因为存在很多不同的 crt 版本——不可能说,哪个库将包含这个符号。假设这可以是 msvcurt[d].lib
或 vcruntime[d].lib
。可能在 msvcrt.lib
。所有你需要的 - 添加这个库之一到 linker 输入。他找到了这个符号
我在 Visual studio 和 CodeBlocks 上使用 Clang 5.0 平台工具集 LLVM-VS2014 时遇到了这个问题。
我添加了 msvcrt.lib 以提供缺少的 __imp____RTDynamicCast。
这个应该怎么推导出来,我还没看懂:-(