打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c++

Whilst compiling with avr-gcc I have encountered linker errors such as the following:

undefined reference to `__cxa_pure_virtual'

I've found this document which states:

The __cxa_pure_virtual function is an error handler that is invoked when a pure virtual function is called.

If you are writing a C++ application that has pure virtual functions you must supply your own __cxa_pure_virtual error handler function. For example:

extern "C" void __cxa_pure_virtual() { while (1); }

Defining this function as suggested fixes the errors but I'd like to know:

  • what the purpose of this function is,
  • why I should need to define it myself and
  • why it is acceptable to code it as an infinite loop?
asked May 28 '09 at 11:55
Matthew Murdoch
14.5k1867105
    
You may get this error when using the gcc command where you should be using g++; I've done it at least once :) – user2023370 May 25 at 15:04

2 Answers

up vote 20 down vote accepted

If anywhere in the runtime of your program an object is created with a virtual function pointer not filled in, and when the corresponding function is called, you will be calling a 'pure virtual function'.

The handler you describe should be defined in the default libraries that come with your development environment. If you happen to omit the default libraries, you will find this handler undefined: the linker sees a declaration, but no definition. That's when you need to provide your own version.

The infinite loop is acceptable because it's a 'loud' error: users of your software will immediately notice it. Any other 'loud' implementation is acceptable, too.

answered May 28 '09 at 12:02
xtofl
24.8k648114
    
What could cause the virtual function pointer to not be filled in? Wouldn't the compiler detect this and prevent compilation? – Matthew Murdoch May 28 '09 at 12:11
1  
:) have a look into the 'related' questions on the right. – xtofl May 28 '09 at 12:43
    
@xtofl Thanks, stackoverflow.com/questions/99552/… was particularly helpful. – Matthew Murdoch May 28 '09 at 14:00

1) What's the purpose of the function __cxa_pure_virtual()?

Pure virtual functions can get called during object construction/destruction. If that happens, __cxa_pure_virtual() gets called to report the error. See Where do "pure virtual function call" crashes come from?

2) Why might you need to define it yourself?

Normally this function is provided by libstdc++ (e.g. on Linux), but avr-gcc and the Arduino toolchain don't provide a libstdc++.

The Arduino IDE manages to avoid the linker error when building some programs because it compiles with the options "-ffunction-sections -fdata-sections" and links with "-Wl,--gc-sections", which drops some references to unused symbols.

3) Why is it acceptable to code __cxa_pure_virtual() as an infinite loop?

Well, this is at least safe; it does something predictable. It would be more useful to abort the program and report the error. An infinite loop would be awkward to debug, though, unless you have a debugger that can interrupt execution and give a stack backtrace.

answered Feb 20 '12 at 2:38
Mark Seaborn
63977
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
日志里背景音乐设置
超实用!单片机Fault故障常见应对办法
Abstract Classes
Mindjet 11 RunTime Error, R6025, pure virtual function call 移动文件后打开出错
Shared Libraries without an MMU
R6025 runtime error :pure virtual funcion call 原因 (From MSDN)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服