HACKER Q&A
📣 pipeline_peak

What are a compilers limits on hardware


I’ve noticed in embedded systems, C compilers are primarily used. People often claim this is because a C++ compiler would be too big, the language has more features.

Does this mean that C++ is inherently limited with underpowered hardware. Or even that concepts like OOP make use of virtual tables and other memory overheads. Or is it simply because a C++ compiler is more difficult to implement for a specific architecture, so why bother when C is enough for system programming, etc.


  👤 duped Accepted Answer ✓
One big reason why compilers don't target architectures is because you have to write codegen and an assembler for it - the first is hard and requires intimate knowledge of the ISA and the second is tedious since there are few assembler libraries at all. So you use an existing compiler infrastructure like gcc or llvm that does it for you.

For C++ specifically it's a great language for low resource embedded systems and widely used. There is nothing stopping you from using it today (responsibly).

A much more interesting road is languages like Java that have more intense runtime requirements like garbage collection. But Java has run in similar environments for decades - it just takes the right VM and some restrictions (usually, no reflection).

A very interesting project right now is Toit, which is pushing the boundary on JIT compilation on small devices.


👤 mikewarot
Arduino, one of the most popular starter embedded systems, uses C++ for its programming.

The main issue in embedded systems is the lack of RAM and the hard real-time requirements that make using garbage collected languages sometimes problematic.

When you get down to really small systems, you're writing in assembler to try to fit code in the N bytes available. I once crammed code into a 512 byte space, when I was done, I had added features requested by the customer, and had 1 byte left over. Whew!

Thankfully those days are pretty much over, unless you're programming a 3 cent microcontroller.


👤 detaro
It's a mix of three things: a) C++ compilers are complex, and vendors want to invest as little as possible in tooling, so C as they've always done it is. And you can't indeed make use of all C++ features on limited platforms, which b) leads to people misrepresenting it as "C++ being useless" based on wrong impressions what C++ can and can't do and c) on extremely small targets indeed reduces how much value you get out of using C++ over C.