Many users are not aware of the fact that C and C++ are two different languages, governed by two different standards. It may appear, that C is just a subset of C++ - and, indeed, C++ started off as a set of extensions to C. However, during the decades these two languages - and especially C++ - evolved so that they do differ in quite a couple of small, but important ways. This results in different behaviour when using C++, even if it is used in a way which would appear to be "fully compatible" with C, and even without a single change in the source files when migrating between C and C++.
One of those differences comes up quite often in microcontroller programming and demonstrates itself as interrupts not working properly. Namely, interrupts working flawlessly in C now result in an infinite loop (usually in something like DefaultHandler) in C++.
The reason for this problem is, that C++ mangles the names of functions - the reasons for this are manyfold and are quite well explained in this wikipedia article.
The underlying mechanism of the problem is simple, but has a relatively complex reason. As correct invocation of interrupt handler upon occurence of interrupt in STM32 (and several other mcu) relies on inserting the proper interrupt service handler (ISR) function's address (called also interrupt vector) into a table (called vector table) at a fixed position in memory, translation environments usually accomplish this by setting up such table with pre-established ISR names, often in the startup code (which is usually added to the translation process without user being specially notified, to provide the "C source is enough" experience to the user).
As the mangled ISR name coming out of C++ translation does not match the pre-established ISR name in the vector table anymore, linker treats it as a normal function (and as it is then not called from anywhere, it may even entirely optimize it out). The ISR's address is thus not inserted to the vector table properly.
The startup code usually contains also a stub implementation of every ISR, these stubs marked as weak (so that linker can override it by the "real" ISR when defined by the user). Also, this stub implementation often is a single catch-all function usually called "DefaultHandler" or something similar, and contains nothing but an infinite loop. So, as a consequence of this all, when such an interrupt is invoked, the execution surprisingly ends up in that inifinite loop, rather than in the ISR written by user.
C++ authors anticipated this problem and C++ provides a mechanism to prevent function names from being mangled. This is accomplished by enclosing given ISR into a block marked as extern "C":
extern "C" {
void TIM2_IRQHandler() {
[...]
}
}
"Libraries" such as Cube/HAL may contain prototypes of ISR functions, already containing the extern "C" mark, in their .h header files; and then in the definition of such ISR there's no need to mark them again. However, this mechanism then relies on having properly included given header containing said prototypes, which is then subject to the usual complex ways how such "magic" mechanisms can break down, admittingly rarely, but frustratingly.
Occurence of this problem may be diagnosed by observing whether the proper ISR address is inserted into the respective position in the vector table in the final executable binary, using disassembler or disassembly view in a graphical IDE.