Microcontrollers using C or C++

27,937

Solution 1

C is more low-level and does just exactly what you say. It is more adapted to low-resources environments such as micro-controllers.

C++ has some features which requires additional resources (such as OOP, exception, and so on).

Moreover the micro-controller does not have the same features as your computer's CPU. It could for example not support dynamic library loading and even for static libraries you're limited in size as your chip doesn't have many memory.

Usually, micro-controllers expose special input/output library, and the stdlib is not always available.

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

If the chip supports it, you can re-compile the stdlib to use the standard C features, and then you can eventually (once again if the chip has enough resources) build a C++ cross-compiler and then the STL. Then you will be able to build C++ program on your chip, but the program will weight much more than the original C program.

Solution 2

Some C++ features like exceptions and virtual functions can add overhead to your program which is undesirable in highly resource constrained environments. This reduces the demand for C++ compilers on such platforms. It is also much more difficult to implement a C++ compiler than a C compiler. This difficulty plus lack of demand makes it so many micro-controllers only have C compilers available for them.

I would learn C for your micro-controller programming. It is not difficult to learn C after learning C++ and will be much easier to code in than assembly.

Solution 3

It is merely historical accident and practice (by old-time Luddites like me) that ucontrollers "prefer" ASM and C. If your compiler can compile C++ into ucontroller code, there's no theoretical reason that I know of why you should not use C++.

To me, it's much easier and more natural to use ASM and C but you can use whichever you prefer so long as your compiler (and linker, if you use it) can do the right thing; and your ucontroller has enough memory to accomodate the (perhaps bigger) compiled C++ code.

Solution 4

It's just the availability of resources, really, as explained by the other posters. By the time you've compiled in a couple virtual method tables and a couple dozen object pointers, that's all the RAM gone from a simple uC!

That said, I prefer C++ on today's 32-bit controllers with 8K upwards of RAM, plenty of flash, complex embedded peripherals and multitasking libs. After decades of OO, using plain C is nightmarish for anything non-trivial.

I currently use NXP ARM chips & Rowley Crossworks, (IDE, uses gcc). I only use C for lib interfaces and assembler for some drivers, all the rest is C++.

Solution 5

Microcontrollers are memory and bandwidth constrained processing units. C programming language generates tight code that is close to assembly language in terms of size and speed. C++ usually carries an overhead in memory and speed. Another issue is dynamic memory allocation. Using object oriented design with C++ usually implies dynamically creating and destroying objects. Embedded applications using microcontrollers, typically allocate all the required memory statically and is not freed up for the life time of the application.

That being said, if you are using a 32 bit microcontroller and your application is complex enough that it handles either lot of data traffic or has significant user interface via touch screen / LCD etc., C++ (& sometimes even C# ) is the language of choice.

The compiler that you choose would depend on the microcontroller, check the microcontroller vendor website for the appropriate development tool suite to use.

Assembly language is used only for the lowest layers if it cannot be done in C. It is harder to maintain and port assembly language code, hence it is best to minimize its use in your application.

Share:
27,937
Ju-chan
Author by

Ju-chan

A software engineer for a living.

Updated on December 31, 2020

Comments

  • Ju-chan
    Ju-chan over 3 years

    Possible Duplicate:
    Is there any reason to use C instead of C++ for embedded development?

    I'm very curious about this: Why is it that when we deal with microcontrollers, they prefer C instead of C++? Based on my researches, C and Assembly language is the usual programming language for these devices. I only know C++ and Assembly Language. So in this case, should I start learning C or stick with Assembly language and if so, what compiler should I use because I only know the Turbo Assembler.

    Thanks and more power! :)

  • Hassan Faghihi
    Hassan Faghihi over 5 years
    so there are chip that support C++? are they the regular ones? or the rare ones? is it good to learn C++ since it's well structured? or just go for the spagety C?
  • Geoffroy
    Geoffroy over 5 years
    @deadManN it's not about supporting C or C++, you just need a compiler which is able to produce binary code compatible with your chip. C++ adds some overhead which increases your binary size and potentially the computation time for some features, which is why C++ support is usually limited in compilers targeting ucontrollers. If I remember correctly, the arduino environment by default supports C++.
  • Hassan Faghihi
    Hassan Faghihi over 5 years
    any good tutorial?