我只是遇到了以下错误:
(.gnu.linkonce。[内容]):定义 引用[方法][对象] 文件:(.gnu.linkonce。[内容]): 对' typeinfo for '的未定义引用 (名称)的
为什么可能会得到这些“未定义的引用typeinfo”链接错误之一? 有人能解释一下幕后发生了什么吗?
我只是遇到了以下错误:
(.gnu.linkonce。[内容]):定义 引用[方法][对象] 文件:(.gnu.linkonce。[内容]): 对' typeinfo for '的未定义引用 (名称)的
为什么可能会得到这些“未定义的引用typeinfo”链接错误之一? 有人能解释一下幕后发生了什么吗?
当前回答
I encounter an situation that is rare, but this may help other friends in similar situation. I have to work on an older system with gcc 4.4.7. I have to compile code with c++11 or above support, so I build the latest version of gcc 5.3.0. When building my code and linking to the dependencies if the dependency is build with older compiler, then I got 'undefined reference to' error even though I clearly defined the linking path with -L/path/to/lib -llibname. Some packages such as boost and projects build with cmake usually has a tendency to use the older compiler, and they usually cause such problems. You have to go a long way to make sure they use the newer compiler.
其他回答
检查你的依赖是在没有-f-nortti的情况下编译的。
对于某些项目,你必须显式地设置它,比如在RocksDB中:
USE_RTTI=1 make shared_lib -j4
与上面的RTTI、NO-RTTI讨论类似,如果使用dynamic_cast而未能包含包含类实现的目标代码,也会发生此问题。
我在Cygwin上构建代码时遇到了这个问题,然后将代码移植到Linux上。make文件、目录结构甚至gcc版本(4.8.2)在这两种情况下都是相同的,但是代码在Cygwin上可以正确链接和操作,但在Linux上却无法链接。Red Hat Cygwin显然已经对编译器/链接器进行了修改,以避免目标代码链接需求。
Linux链接器错误消息正确地将我引导到dynamic_cast行,但本论坛早期的消息让我寻找缺少的函数实现,而不是实际的问题:缺少目标代码。我的解决方法是在基类和派生类中替换一个虚拟类型函数,例如virtual int isSpecialType(),而不是使用dynamic_cast。这种技术避免了仅仅为了使dynamic_cast正常工作而链接对象实现代码的需求。
I encounter an situation that is rare, but this may help other friends in similar situation. I have to work on an older system with gcc 4.4.7. I have to compile code with c++11 or above support, so I build the latest version of gcc 5.3.0. When building my code and linking to the dependencies if the dependency is build with older compiler, then I got 'undefined reference to' error even though I clearly defined the linking path with -L/path/to/lib -llibname. Some packages such as boost and projects build with cmake usually has a tendency to use the older compiler, and they usually cause such problems. You have to go a long way to make sure they use the newer compiler.
在我的例子中,它是一个接口类中的虚函数,没有定义为纯虚函数。
class IInterface
{
public:
virtual void Foo() = 0;
}
我忘记了= 0位。
前面的答案是正确的,但是这个错误也可能是由于试图对没有虚函数的类的对象使用typeid而导致的。c++ RTTI需要虚表,因此希望对其执行类型标识的类至少需要一个虚函数。
如果希望类型信息作用于一个实际上不需要任何虚函数的类,可以将析构函数设为虚函数。