Skip to content

BLOG

Pros and Cons of C++ for Embedded Systems

When it comes to IoT projects, there’s a lot of stuff a developer needs to understand, and a lot of languages that are beneficial to know as you’re architecting IoT hardware, software, firmware, and everything in between. C++ is among the most widely used of these languages, and it has some compelling benefits as well as some considerable drawbacks for IoT development.

My first development mentor, an ex-Googler currently engineering at Facebook, accomplished much of his work using C++, so it was the first programming language I learned personally. Based on my experience throughout my career and here at Very, I’ve come up with several observations about C++ in particular.

Created by computer scientist Bjarne Stroustrup in 1985, C++, as its name suggests, is an extension of the C programming language that appeared 13 years earlier in 1972. C was originally created to improve upon its predecessor, B, which in turn had replaced assembly language to accomplish machine learning in the UNIX operating system.

Though C and C++ have many features in common, like the highly valuable functional and structural paradigms that old-school C establishes, modern C++ enhances those paradigms by making object-oriented programming possible within the language.

Pros of C++ for Embedded Systems

By design, C++ lends itself to embedded development because the language sits in between higher-level software and hardware, allowing you to access and control hardware directly without sacrificing the benefits of a high-level language.. It’s particularly effective for hardware that will need to be around for a while, as programs written in C++ can operate for decades at a time due to the language’s high stability.

C++ also gives developers the ability to efficiently use abstractions without too much cost to infrastructure. The data structure of C++, like C, is algorithm-based, which makes it a great choice for solving all the little puzzles you encounter during embedded development. C++ also has processor independence, and microprocessors today come loaded with C++ compilers starting at $1 USD.

C++ is still the standard/default Language to use for embedded development, and will be so for a long time. As our world becomes more connected through IoT, we’ll continue to see C++ crop up all around us. It’s already widely used by major companies like Google, Microsoft, and Oracle. In fact, Google’s own programming language draws heavily from C++.

And while C++ can be difficult for a developer to learn at first (this is one of the cons that we’ll discuss below), once you do learn it, it’s much easier to learn and understand other languages that are influenced by C and C++, like Java, C#, Python, JavaScript, and more.

Cons of C++ for Embedded Systems

As mentioned above, perhaps one of the biggest obstacles for developers and companies who wish to adopt C++ is that it’s a very strict, very verbose, very complex language. This makes C++ quite difficult to learn, even for seasoned developers. To practice embedded development in your company, therefore, you’ll need the budget to recruit and retain developers with C++ knowledge and experience. If you don’t have those expert resources, you’re looking at longer development cycles and slower time to market with C++.

Additionally, because the language is so complex, maintaining your codebase can present significant challenges. We mentioned above that a lot of major companies use C++ – and ironically, another reason for this is that some of them can’t use anything else. If they used C++ to develop critical programs in the 80s and 90s, and it’s still working, there’s not much incentive to update it and risk breaking something.

For companies and areas where it’s possible to update C++ to a simpler language, however, the change may make more business sense and enable more agility. This is especially true where web development is concerned because languages like C++ lag behind web-based software technologies. A single line of code in a web ecosystem, for example, may execute more times in one day than its embedded counterpart will be executed over the entire lifetime of its hardware host. When you’re trying to develop and iterate an IoT application quickly, this can be a huge blocker.

Using Elixir and Nerves for IoT Development

So, if C++ isn’t a perfect fit for all or part of your project, and you have the flexibility to adapt to another language and/or framework, what are the best alternatives? Here at Very, we use C++ in all the places where it makes sense for the project and the business goals, but we’re also huge fans of Elixir and Nerves – and we often use all of them together.

Elixir is a relatively young language built upon the principles of the much older Erlang, which was developed in 1987 for low-level, remote hardware applications. Much like how C++ takes many of its core paradigms from the original C, Elixir mimics Erlang in that it’s fault-tolerant, highly available, and supports “hot-swapping” – i.e., adding and changing program code while the program is running. These traits – plus the fact that Elixir has data immutability, is process-based, and encourages concise, testable code – make it an ideal language for developing embedded firmware.

Nerves, meanwhile, is an IoT development framework that applies the principles of web development, including tools for secure communication with web servers and tools for continuous delivery, to building embedded software in Elixir. When it comes to actually shipping to production and making sure your IoT devices continue to work after you release them into the wild (think: cellular networking, firmware updates, and security), Nerves is a great option because it’s resilient, reproducible, and reasonable. Nerves also offers some secret advantages – namely, that you can use the framework to sneakily trick your Elixir developers into working on embedded systems.

Choosing What’s Right for You

Whichever languages and frameworks you choose for IoT development, it’s critical that you’re thinking about all of the ways your choices can impact your project and ultimately your business. This pros and cons list is a great place to begin your research.

KEEP READING: Find out how Very partnered with Koller Products to develop the world’s first internet-connected desktop aquarium and cross-platform mobile app using a C++ framework.