Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
307 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...