C++ __cxa_pure_virtual 的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/920500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:57:29  来源:igfitidea点击:

What is the purpose of __cxa_pure_virtual?

c++avr-gccpure-virtual

提问by Matthew Murdoch

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

在使用 avr-gcc 编译时,我遇到了如下链接器错误:

undefined reference to `__cxa_pure_virtual'

I've found this documentwhich states:

我找到了这份文件,其中指出:

The __cxa_pure_virtualfunction 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_virtualerror handler function. For example:

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

__cxa_pure_virtual函数是在调用纯虚函数时调用的错误处理程序。

如果您正在编写具有纯虚函数的 C++ 应用程序,您必须提供您自己的__cxa_pure_virtual错误处理函数。例如:

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?
  • 这个函数的目的是什么,
  • 为什么我需要自己定义它
  • 为什么将其编码为无限循环是可以接受的?

采纳答案by xtofl

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.

无限循环是可以接受的,因为它是一个“响亮”的错误:您的软件的用户会立即注意到它。任何其他“大声”的实现也是可以接受的。

回答by Mark Seaborn

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

1) __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?

可以在对象构造/销毁期间调用纯虚函数。如果发生这种情况,会调用 __cxa_pure_virtual() 来报告错误。请参阅“纯虚函数调用”崩溃从何而来?

2) Why might you need to define it yourself?

2) 为什么你可能需要自己定义它?

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

通常这个函数是由 libstdc++ 提供的(例如在 Linux 上),但是 avr-gcc 和 Arduino 工具链不提供 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.

Arduino IDE 在构建某些程序时设法避免链接器错误,因为它使用选项“-ffunction-sections -fdata-sections”进行编译并使用“-Wl,--gc-sections”链接,这会删除对未使用符号的一些引用.

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

3) 为什么将 __cxa_pure_virtual() 编码为无限循环是可以接受的?

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.

嗯,这至少是安全的;它做了一些可预测的事情。中止程序并报告错误会更有用。但是,除非您有一个可以中断执行并提供堆栈回溯的调试器,否则调试无限循环会很尴尬。