5.03 C++ Name Mangling, Type Ambiguity & Integrating C RTOS APIs with extern "C"
π» 1. Annotated Source Code
#include <iostream> #include <string> using namespace std; int getResult(int num1, int num2); string getResult(string str1, string str2); int getResult(int num); int main() { int resultNum = getResult(30, 20); string nameResult = getResult("John", "Baugh"); int cubeResult = getResult(5); cout << "result num is " << resultNum << endl; cout << "name result is " << nameResult << endl; cout << "cube result is " << cubeResult << endl; return 0; } int getResult(int num1, int num2) { return num1 * num2; } string getResult(string str1, string str2) { return str1 + " " + str2; } int getResult(int num) { return num * num * num; }
π 2. Architecture & UML Class Model
π 3. Core C++ Concepts Deep-Dive
1. Function Overloading Resolution
Functions can share the same name if their parameter lists differ in count, types, or constness. Return type alone is insufficient to overload a function.
2. C++ Name Mangling
To differentiate overloaded functions at the object-file level, the C++ compiler encodes parameter types into the symbol name in the compiled binary (e.g., _Z8transmiti vs _Z8transmitPKc).
β‘ 4. Embedded Systems & Hardware Reality
1. Interfacing with C Microcontroller HALs (extern "C")
Most hardware vendor libraries (STM32 CubeHAL, ESP-IDF, FreeRTOS) are written in C. Because C compilers do not mangle symbol names, C++ code calling C functionsβor C code calling C++ interrupt handlersβmust be wrapped in extern "C" to disable name mangling.
π‘ 5. Production-Ready Embedded Refactoring
Robust C/C++ compatible header wrapper:
#ifdef __cplusplus extern "C" { #endif // Hardware ISR handler (must match C symbol name for vector table) void USART1_IRQHandler(void); // FreeRTOS Task Entry Point void vSensorTask(void* pvParameters); #ifdef __cplusplus } #endif
π Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.
extern "C" instructs the C++ compiler to emit unmangled C symbol names, enabling interoperability with C HALs and hardware vector tables.
SysTick_Handler. A mangled C++ symbol (like _Z15SysTick_Handlerv) will not match, leaving the interrupt bound to the default unhandled loop.