Use the right tool for the job: embedded programming

后端 未结 8 1589
[愿得一人]
[愿得一人] 2020-12-23 22:47

I\'m interested in programming languages well suited for embedded programming. In particular:

Is it possible to program embedded systems in C++? Or is it better to u

相关标签:
8条回答
  • 2020-12-23 23:17

    Embedded programming these days spans a large range of applications.
    Roughly, it goes from sensors/switches up to complete security systems.
    You should base your language on the complexity and the hardware resources.
    It is 1 of the choices next to HW (CPU,...), OS, protocols,...
    possible choices:

    • switches: assembler
    • router-like devices: C and/or C++
    • handhelds: Java or QT/C++
    • complete systems: combinations C and/or C++ with python
    0 讨论(0)
  • 2020-12-23 23:23

    Is it possible to program embedded systems in C++?

    Yes, of course, even on 8bit systems. C++ only has a slightly different run-time initialisation requirements than C, that being that before main() is invoked constructors for any static objects must be called. The overhead (not including the constructors themselves which is within your control) for that is tiny, though you do have to be careful since the order of construction is not defined.

    With C++ you only pay for what you use (and much that is useful may be free). That is to say for example, a piece of C code that is also C++ compilable will generally require no more memory and execute no slower when compiled as C++ than when compiled as C. There are some elements of C++ that you may need to be careful with, but much of the most useful features come at little or no cost, and great benefit.

    Or is it better to use pure C?

    Possibly, in some cases. Some smaller 8 and even 16 bit targets have no C++ compiler (or at least not one of any repute), using C code will give greater portability should that be an issue. Moreover on severely resource constrained targets with small applications, the benefits that C++ can bring over C are minimal. The extra features in C++ (primarily those that enable OOP) make it suited to relatively large and complex software construction.

    Or is C++ OK only if some features of the language (e.g. RTTI, exceptions and templates) are excluded?

    The language features that may be acceptable depend entirely on the target and the application. If you are memory constrained, you might avoid expensive features or libraries, and even then it may depend on whether it is code or data space you are short of (on targets where these are separate). If the application is hard real-time, you would avoid those features and library classes that are non-deterministic.

    In general, I suggest that if your target will be 32bit, always use C++ in preference to C, then cut your C++ to suit the memory and performance constraints. For smaller parts be a little more circumspect when choosing C++, though do not discount it altogether; it can make life easier.

    If you do choose to use C++, make sure you have decent debugger hardware/software that is C++ aware. The relative ease with which complex software can be constructed in C++, make a decent debugger even more valuable. Not all tools in the embedded arena are C++ aware or capable.

    I always recommend digging in the archives at Embedded.com on any embedded subject, it has a wealth of articles, including a number of just this question, including:

    • Poor reasons for rejecting C++
    • Real men program in C
    • Dive in to C++ and survive
    • Guidelines for using C++ as an alternative to C in embedded designs
    • Why C++ is a viable alternative to C in embedded systems design
    • Better even at the lowest levels

    Regarding Java, I am no expert, but it has significant run-time requirements that make it unsuited to resource constrained systems. You will probably constrain yourself to relatively expensive hardware using Java. Its primary benefit is platform independence, but that portability does not extend to platforms that cannot support Java (of which there are many), so it is arguably less portable than a well designed C or C++ implementation with an abstracted hardware interface.

    [edit] Concidentally I just received this in the TechOnline newsletter: Using C++ Efficiently in Embedded Applications

    0 讨论(0)
  • 2020-12-23 23:24

    I think you already have a great answer by Clifford, but I'll add my experience to it. As was pointed out, the type of embedded system is the main driver. In Defense/Aerospace, C and Ada are the most popular embedded languages I encounter. Although as time goes on, I am seeing more C++ as the model based development becomes popular with the tools such as Rhapsody. In the jobs listing, seeing requirements for Object Oriented Design experience also leads me to believe that the market is slowly shifting to follow the trends of mainstream development.

    If you're really interested in embedded development, I would start with C. It is pretty universal. Almost every OS, including real time OS's like Integrity, Nucleus, VxWorks, and embedded Linux, has a compiler and tools for it. The things you'll learn about pointers, memory management, etc... will translate into C++ development well in the embedded world at least.

    As for Java, if you're interested in development for mobile platforms like smart phones, this is a solid choice (Android comes to mind). But in the world of Real Time Operating Systems, I haven't seen it.

    On that note, that brings me to my last point and advice. If I wanted to jump into embedded programming (which I did just 4 years ago), I would focus on learning C from a low level point of view as mentioned above. Then, I would also learn what makes real time programming so difficult/different. I found the book below quite good at teaching you to think like an embedded programmer vs. an application developer. Good luck!

    An Embedded Software Primer

    0 讨论(0)
  • 2020-12-23 23:25

    c is the most common language used in embedded systems.

    However, I think C++'s future lies in the embedded system domain. I think the C++0x standards will help in that resepect. So I wouldn't be surprised to see C++ used a lot more in this field.

    0 讨论(0)
  • 2020-12-23 23:31

    More often than not in embedded systems, the language you're programming in is determined by which compiler is actually available.
    If you're hardware only has a C compiler, that's what you're going to use. IF it has a decent C++ compiler than there is really no reason to prefer C over C++.
    I would dare say that Java isn't a very popular choice in embedded systems.

    0 讨论(0)
  • 2020-12-23 23:31

    Both C and C++ can be used on embedded systems. If you do limit the features of C++ that you use, then it is going to use roughly the same speed and space as C. As for using these additional features, it really depends on your constraints. For example, if you are making a real-time system, then exceptions might not be a good idea, simply because considering the propagation time for exceptions and all the paths through which exceptions can possibly propagate can make hard real-time guarantees quite tough (although, then again, the ACE / Tao real-time CORBA implementation uses exceptions). While templates and RTTI can lead to larger programs, there is a lot of variability in embedded systems, and so depending on your resource constraints, this could be perfectly fine or unacceptable. The same goes for Java. Java (well, technically, the Java programming language with a subset of the Java API) is running on Android, for example, using the Dalvik VM. J2ME runs on a variety of embedded devices. Whether it will or will not work for your application, depends on your particular constraints.

    0 讨论(0)
提交回复
热议问题