What is the purpose of __cxa_pure_virtual?
Asked Answered
B

3

66

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?
Behold answered 28/5, 2009 at 11:55 Comment(1)
You may get this error when using the gcc command where you should be using g++; I've done it at least once :)Bove
D
46

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.

Deserve answered 28/5, 2009 at 12:2 Comment(2)
What could cause the virtual function pointer to not be filled in? Wouldn't the compiler detect this and prevent compilation?Behold
@Deserve Thanks, #100052 was particularly helpful.Behold
C
29

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.

Campagna answered 20/2, 2012 at 2:38 Comment(0)
T
0

In my case I used clang, instead of clang++. clang++automatically links the libstdc++. A similar problem might be using gcc instead of g++.

Tadpole answered 15/7 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.