Microcontrollers using C or C ++

Possible duplicate:
Is there a reason to use C instead of C ++ for embedded development?

I am very interested: why is it that when we deal with microcontrollers, they prefer C instead of C ++? Based on my research, C and assembly language is a common programming language for these devices. I only know C ++ and Assembly. Therefore, in this case, you should start learning C or stick with assembly language, and if so, which compiler should I use, because I only know Turbo Assembler.

Thanks and more power! :)

+7
source share
6 answers

C is lower level and does exactly what you say. It is more adapted to low-resource environments such as microcontrollers.

C ++ as some functions that require additional resources (such as OOP, exception, etc.).

In addition, the microcontroller does not have the same functions as your computer. This may, for example, not support loading a dynamic library, and even for static libraries that are limited in size, since your chip does not have a lot of memory.

Microcontrollers typically provide a dedicated I / O library, and stdlib is not always available.

What you need is a cross-compiler for your microcontroller. Then you can write your program in C and ASM.

If the chip supports it, you can recompile stdlib to use the standard C functions, and then you can (again, if the chip has enough resources) build a C ++ cross-compiler, and then STL. Then you can create a C ++ program on your chip, but the program will weigh much more than the original C program.

+2
source

Some C ++ functions, such as exceptions and virtual functions, can add extra overhead to your program, which is undesirable in resource-limited environments. This reduces the demand for C ++ compilers on such platforms. In addition, it is more difficult to implement the C ++ compiler than the C compiler. This complexity, plus the lack of demand, makes many microcontrollers only have C compilers.

I would learn C to program your microcontroller. It's easy to learn C after learning C ++ and it will be much easier to code than assembly.

+10
source

It’s just a historical coincidence and practice (for old Luddites like me) that ucontrollers β€œprefer” ASM and C. If your compiler can compile C ++ to ucontroller code, there is no theoretical reason why I should not use C ++.

It is much easier and more natural for me to use ASM and C, but you can use what you prefer as long as your compiler (and the linker, if you use it) can do the right thing; and your ucontroller has enough memory to accommodate (possibly larger) compiled C ++ code.

+3
source

Microcontrollers are processing units with limited memory and bandwidth. The C programming language creates hard code similar to assembly language in terms of size and speed. C ++ usually carries overhead in memory and speed. Another problem is dynamic memory allocation. Using object-oriented design with C ++ usually involves dynamically creating and destroying objects. Embedded applications using microcontrollers, as a rule, allocate all the required memory statically and are not freed throughout the life of the application.

If you use a 32-bit microcontroller, and your application is quite complicated, because it processes either a lot of data traffic, or has a significant user interface via a touch screen / LCD, etc., C ++ (and sometimes even C #) , is the selected language.

The compiler you choose will depend on the microcontroller; check the website of the microcontroller vendor for the appropriate development toolkit.

Assembly language is used only for the lowest levels, if this cannot be done in C. It is more difficult to maintain and display assembly language code, so it is best to minimize its use in your application.

+2
source

Microcontrollers are small devices that are not very powerful compared to computers. They have limited resources. Firstly, the stack size is very limited, so it is not recommended to have many nested function calls (on some devices, stacks are limited to a few bytes). Secondly, it is often impossible to dynamically allocate memory (alloc, free ...), and most of the program data must be global static variables or stored on the stack, so useful classes such as std::vector will not be available.

Even if C ++ compilers can be used for microcontrollers, this would not be very useful, since the low capabilities of these devices would prevent the simple use of this powerful language. Using C is often easier for simple tasks, and microcontrollers are sized for simple tasks.

+2
source

It's just the availability of resources, indeed, as other posters have explained. By the time you assembled a couple of virtual method tables and a couple of dozen object pointers, all RAM had disappeared from simple uC! A.

However, I prefer C ++ on today's 32-bit controllers with 8 Kbytes of memory, lots of flash memory, sophisticated embedded peripherals and multitasking libraries. After decades of OO, using plain C is a nightmare for something non-trivial.

I am currently using NXP ARM and Rowley Crossworks chips, (IDE, uses gcc). I use only C interfaces for lib and assembler for some drivers, everything else is C ++.

+2
source

All Articles