C vs C++ on ARM microcontrollers

Discussion in 'Embedded Systems and Microcontrollers' started by tgil, Feb 15, 2013.

  1. tgil

    Thread Starter New Member

    May 18, 2011
    19
    4
    I am looking for anyone to chime in on what programming language they use on ARM microcontrollers (STM32, LPC, Stellaris, etc): C or C++. I have always used C, but C++ has some great advantages. What do you think?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    C++ is an extension of C. Why would you want to use C++ in an embedded system?
     
  3. tgil

    Thread Starter New Member

    May 18, 2011
    19
    4
    Great question. I know why I am interested in using C++ (stronger type checking, OOP model), but I want to hear more from the community about what they are using.

    I suppose we have one vote for C so far.
     
  4. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,906
    2,162
    My 2c worth.
    Most of the programming logic that would be running on a micro-controller is procedural in nature so C makes a good fit because of its smaller footprint as a bit-banging language. IMO pushing the OOP model into this class of programming problems seems to be driven by dogma instead of practical application and most of what's possible in C++ on the small uC level (Modular design ,Encapsulation,Polymorphism) is possible within C with proper coding.
     
    Last edited: Feb 15, 2013
  5. MrChips

    Moderator

    Oct 2, 2009
    12,418
    3,355
    I am in the middle of writing a GUI on an ARM chip.
    It includes windows, icons, buttons, menus etc.
    I could benefit by using OOP but I can manage just fine in C.

    I should say the GUI part is 95% completed. Total code space is 30kB.

    Just my 1-C worth.
     
    Last edited: Feb 15, 2013
  6. tgil

    Thread Starter New Member

    May 18, 2011
    19
    4
    Cpp is certainly not going to add much benefit for small mcu projects (say less than 16kb). But mid size (64k to 1mb) could certainly benefit from cpp.

    We now have 2 votes for c and perhaps a half vote for cpp.
     
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    When working with multi user, or graphical UI systems, Object Oriented makes some tasks simpler, at the price of a much higher footprint.

    Even programs in "C++" for microcontrollers use the standard C library, rather than object methods and properties. It's only when doing that gets "hard" do people make an object and mix it in with the rest of the code.

    Arduino is a great example. It was built to be programmed in C++, but nearly all applications use only the C functions unless dealing with a serial instance. In fact, the arduino forum recommends Against using the String object in favor of the standard C library string functions, especially when running out of program space.

    So, C for embedded. When the user is messing with a GUI and changing apps, the application is no longer embedded, the user knows they are working with a computer.
     
  8. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    C or C++ is irrelevant.

    It depends what you want to program.

    C++ is just a way of doing things in C, not a new language. Early C++ compilers boiled down the C++ constructs to C.

    For small programs, single task, single user, C++ does not make too much sense.

    If you have more than 100 MHz, and some 100K memory + a large RAM, you can spawn stuff on the heap.

    With a KByte of RAM, how can you do this?

    All you can do is to use static C++ for class declarations. As I say, just a way of using C.

    The descision what you use should be made by the type of application.

    Unless you deal with a larger controller with >100 MHz, MCUs are often pretty much maxed out for performance, or the clocking is reduced to save power. C++ is working in the opposite direction.

    It makes sense for a GUI like Windows, or a system which resembles it.

    Talk about your software projects, and then about C/C++ on microcontrollers.

    Or do you just want to learn C++? A microcontroller is not ideal for that.
     
  9. tgil

    Thread Starter New Member

    May 18, 2011
    19
    4
    I actually do a lot of c programming on micro controllers. There are few things about the language that I don't know. However, there is a lot about cpp that I don't know. Recently, I have experimented with squeezing the cpp footprint by telling the compiler to omit exceptions and run time type information. The footprint does get down to a manageable size. However any use of the standard template libraries can quickly make it balloon again.

    I am working on with an arm cortex m3 with a 120mhz clock and 64k of ram. It is plenty to run cpp.

    Arduino is an interesting example. The original arduino probably should not be running cpp, but the cortex m3 version should be able to.

    I think cpp has some powerful features that allow for greater re-use ability of code as compared to c. I don't know enough about cpp to know if these features require a lot of runtime resources that would prohibit their use in embedded systems.
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I have a STM32 board here with 170MHz, and I think 512K Flash.

    So this could be used with C++ maybe. I made one compiler working but I think limited to 32K or something. C compiler not C++.

    What software do you use for C++?

    I use PIC16, PIC18, and also soon PIC32.

    Think what I have done with a small TFT and a 18F PIC (writing BMP files as binary images to serial FLASH chips).

    Think of the Windows GDI and all the steps I must take to display a bitmap. Ensuring compatibility with any hardware vendor.

    C++ is really neccessary for a multitasking OS- not just to reuse code but also to virtualize the hardware.

    Can be done without C++, but OOP is good to use for that.

    Bought a STM8 kit and a few chips yesterday- the STM32 board is far too powerful for me to use at the moment.

    When I want max. performance, I do not want type checking on a dynamic memory object. I do a typecast on static data...

    It is different if you have effectively 12 MHz at 8 bits, or if you have more than 100 MHz at 32bits.
     
  11. dwuk

    New Member

    Dec 9, 2013
    1
    0
    I recently used Cortex M4 for a project in C++ which was the first time for an embedded project.

    I actually did a lot a testing because I was nervous about the move. I can say in my experience I only gained from using C++.

    As someone mentioned about if you declare all objects statically, and used classes then its just a way of using C.

    Since the project was complex and large, then C++ made sense. It might have had a slightly larger footprint I cant remember, but I can conclusively say that this being a performance intensive project C++ actually gave me some performance improvement over C, exactly the same code the only difference was that calls were being made within a class rather than globally as in C.

    Can I also say that all C code compiles on the C++ compiler. So I see it as just an extention.

    For me just to use it like C but with the advantages of classes makes for far better design and maintainability.

    Yes you can do all of these things in plain C. If you use C++ I beleive these features of class, inheritance and encapsulation are less easily broken by other engineers and your intention to use these features is clearly defined.

    For a very small project i'd probably stick with C. But for larger complicated designs I would now always use C++. It just becomes a lot more manageable as the project grows in requirements and complexity.

    But for small microcontrollers, stick to statically declared objects.

    1 vote for C++ if the project is complex.
     
Loading...