What is the purpose of __cxa_pure_virtual?

前端 未结 2 1839
不知归路
不知归路 2020-12-04 11:19

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

undefined reference to `__cxa_pure_virtual\'

I\'ve foun

2条回答
  •  [愿得一人]
    2020-12-04 11:48

    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.

提交回复
热议问题