Call to virtual function write in Print class causing crash

While I have a lot of embedded C experience I'm not a C++ guru by any means but I've spent the past few days doing some research into this problem I'm having, learning about classes, inheritance and polymorphism. I think I understand how everything should work, so I'm at a loss as to why things aren't working!

I'm currently trying to write a custom implementation of HardwareSerial for a piece of custom hardware I'm working on. Right now I have a completely minimal stripped down version of the class hierarchy and I cannot get the basic functionality to run. The processor crashes in almost every scenario (not sure where it is stuck but it appears to be halted probably in a default error handler).

The Print class, unedited from Arduino, defined the 'write' function as pure virtual, so I define it in the child class (HardwareSerial) but the processor crashes as soon as 'write' is called from the Print class. I can however successfully call it directly using Serial.write('!').

If I make 'write' not be pure virtual and define a default function in the base class (Print), the processor crashes in the exact same way (even if 'write' is completely removed from the child class).

It only succeeds if I don't declare 'write' as virtual at all (in which case the default version I created in the base class gets called.)

It compiles and links without any errors (arm gcc) and the normal Arduino core (plus a multitiude of other cores I looked at for reference) all seem to work, so what am I missing?

try examples of my StreamLib there are classes derived from Print and it works for SAMD, nRF5, esp,...

Have you got a small test sketch for your libraries?

I was able to resolve the issue, it looks like I wasn't correctly enabling global constructors since this was originally a C project and I'm using the GCC compiler. I just had to make sure __libc_init_array() was called in the startup routine.

Interesting discussion on it here ->