Why C++ is Not Recommended for Microcontroller Programming

Follow+Star Public Account, don’t miss out on exciting content
Why C++ is Not Recommended for Microcontroller Programming
Author | strongerHuang
WeChat Public Account | Embedded Column
Generally speaking, when programming on microcontrollers, either assembly or C language is used, and it is rare to develop with C++. So why is it not recommended to use C++ for microcontroller development?
Below, I will discuss the content of writing a flashing light program using C++ in conjunction with Keil and STM32, to see why C++ is not recommended.

Why C++ is Rarely Used for Microcontroller Development

I wonder how much everyone understands about procedural programming and object-oriented programming?
C language is a procedural language, while C++ is an object-oriented programming language. In relation to this article, procedural programming generates a smaller code size (bin file) and has higher execution efficiency compared to object-oriented programming.
Therefore, C language has a smaller code size (bin file) and faster execution speed compared to C++.
Of course, this is in comparison to C++. In fact, assembly language has an even smaller code size and faster execution speed compared to C. You can refer to the article: What is the difference between writing a flashing light program in assembly and C language?
Due to the limited RAM and Flash resources of microcontrollers and their relatively low operating speed, you will find that very few people develop projects using C++ on microcontrollers.
Actually, with the increase in storage resources and operating speeds of microcontrollers, some engineers have started to use C++ for microcontroller projects.

Preparation Work

In the Keil MDK development environment, using C language to develop STM32 has become quite common. Steps such as installing the development environment and creating a project will not be discussed here; please refer to my articles:
Keil Series Tutorial 01_Introduction, Download, Installation, and Registration
Keil Series Tutorial 02_Creating a Basic Software Project
This is the basic tutorial for Keil. If there are any students who do not understand, you can reply with the keyword Keil Series Tutorial in my public account backend to read more about how to use Keil.
Then you need to understand some basic syntax of C++. The content discussed in this article is quite basic and uses fundamental knowledge of C++. For example: classes, objects, and other basic content.
If you haven’t learned C++, it’s okay. As long as you understand C language, learning the basics of C++ online is not difficult.

Explanation

In the Keil MDK environment, the ARM compiler (Arm Compiler) is used. Many online tutorials use V6 compilation; in fact, both V5 and V6 versions support C++ programming language.
Adding C++ source code (for example: main.cpp) to the project
Why C++ is Not Recommended for Microcontroller Programming
There are some differences in project configuration options between V5 and V6:
Why C++ is Not Recommended for Microcontroller Programming
The ST development library has already provided support, and you will see a piece of code like this:
#ifdef __cplusplus extern "C" {#endif
// C source code here
#ifdef __cplusplus}#endif
What does this mean?
This is a piece of preprocessing code that roughly means: support for mixed programming in C and C++.

Defining the LED Class

In this article, it is assumed that everyone has mastered the knowledge of using C language to develop STM32 flashing lights, and I will directly describe the C++ code content.
This article discusses a very basic example “LED Lighting”. There are many methods to implement this using C++ programming, and here is one of the basic methods.
First, create a main.cpp source code file and define an LED class:
class LED_Class{}
Then define private members (of course, public can also be used):
class LED_Class{private:  GPIO_TypeDef *GPIOx;  uint16_t GPIO_Pin;  uint32_t RCC_APB2Periph;}
Next, define the functions used: initializing GPIO, turning on, and turning off the LED, etc.
class LED_Class{private:  GPIO_TypeDef *GPIOx;  uint16_t GPIO_Pin;  uint32_t RCC_APB2Periph;
public:  LED_Class(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin, uint32_t RCC_APB2Periph){    LED_Class::GPIOx = GPIOx;    LED_Class::GPIO_Pin = GPIO_Pin;    LED_Class::RCC_APB2Periph = RCC_APB2Periph;  }
  void Init(void){      GPIO_InitTypeDef GPIO_InitStruct;
      RCC_APB2PeriphClockCmd(RCC_APB2Periph, ENABLE);          GPIO_InitStruct.GPIO_Pin   = GPIO_Pin;      GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;      GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;      GPIO_Init(GPIOx, &GPIO_InitStruct);  }
  void Open(void){    GPIO_SetBits(GPIOx, GPIO_Pin);  }
  void Close(void){    GPIO_ResetBits(GPIOx, GPIO_Pin);  }};
This code is quite simple, and even if you don’t know C++, as long as you understand C language, you should be able to understand it.

Implementation of Flashing Lights (main function)

The thought process in C++ is similar to that in C: first initialize, then implement:
int main(void){  LED_Class LED1(GPIOF, GPIO_Pin_7, RCC_APB2Periph_GPIOF);  LED_Class LED2(GPIOF, GPIO_Pin_8, RCC_APB2Periph_GPIOF);
  LED1.Init();  LED2.Init();
  while(1)  {    LED1.Open();    LED2.Open();    Delay(10);
    LED1.Close();    LED2.Close();    Delay(10);  }}
There is also another way to initialize:
int main(void){  LED_Class *LED1 = new LED_Class(GPIOF, GPIO_Pin_7, RCC_APB2Periph_GPIOF);  LED_Class *LED2 = new LED_Class(GPIOF, GPIO_Pin_8, RCC_APB2Periph_GPIOF);
  LED1->Init();  LED2->Init();
  while(1)  {    LED1->Open();    LED2->Open();    Delay(50);
    LED1->Close();    LED2->Close();    Delay(50);  }}
You will find that the compiled code size is actually larger:
Why C++ is Not Recommended for Microcontroller Programming
The larger code size is one reason, but the more important reason is the development of microcontrollers. Historically, they have been developed using C language, and many existing software and mature solutions are based on C language. You can understand that the ecosystem of microcontrollers still belongs to C language, rather than C++.
———— END ————
Why C++ is Not Recommended for Microcontroller Programming
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from Embedded Column
Follow the public account Reply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Why C++ is Not Recommended for Microcontroller Programming
Why C++ is Not Recommended for Microcontroller Programming
Click “Read the Original” to see more shares.

Leave a Comment

Your email address will not be published. Required fields are marked *