小尺寸微控制器上的 C++

2022-01-25 00:00:00 oop embedded c++

在我看来,人们似乎一直在回避,或者更确切地说,强烈反对在微控制器上使用 C++,但我终其一生都无法弄清楚原因.如果您远离大型 C++ 库(例如 STL)并且不尝试使用 RTTI 或异常处理等复杂功能,那么 C 与 C++ 之间真的有任何明显的区别吗?虚拟继承对复杂性或占用空间有巨大影响吗?我认为这将是一个额外的内存,但大部分复杂性将由编译器处理,但话又说回来,我对那个黑魔法知之甚少.我只是不明白为什么人们非常坚持使用 C,除了可能没有 C++ 编译器的少数架构(如果有的话).即使您不能使用 cin 或 cout,模块化和模板的好处似乎也是显而易见的.

It seems to me people consistently shy away from, or rather vehemently oppose the use of, C++ on microcontrollers, but I can't for the life of me figure out why. If you stay away from large C++ libraries (e.g. STL) and you don't try to use complicated features like RTTI or exception handling, is there really any noticeable difference between C vs C++? Does virtual inheritance have a huge impact on complexity or footprint? I would think it'd be a little extra memory, but most of the complexity would be handled by the compiler, but then again I don't know a lot about that dark magic. I just don't understand why people are pretty adamant about using C, except maybe for the few architectures for which there aren't C++ compilers (if there are any). It seems the benefits of modularization and templates would be a no-brainer, even if you couldn't use your cin or cout.

我问是因为我正在为一些我想从事的业余项目做一些研究.理想情况下,我想严格使用 C++,以便能够很好地模块化事物,而不是 C 的SomeClass_SomeMethod(struct object* this ...)"方法来实现面向对象".(对于这些项目,我更喜欢对象 Pascal,但唉,对该语言的支持并不完全出色......)我宁愿避免转向功能更强大的微处理器,因为 A. 对于我正在做的项目,我不需要大量资源.我不打算编写 60 个状态卡尔曼滤波器或编码 1080p 视频 B.(真正的踢球者)我想使用 DIP 和 QFP 封装中可用的处理器.我希望无需在我的烤箱中焊接或烘烤任何东西即可制作原型.

I ask because I'm doing some research for some hobby projects I'd like to work on. Ideally, I'd like to work with C++ strictly for the capability to nicely modularize things, vs. C's "SomeClass_SomeMethod( struct object* this ... )" approach to "object orientedness". (I'd much prefer object Pascal for these projects, but alas support for that language isn't exactly stellar...) I would rather avoid moving to a much more capable microprocessor because A. for the projects I'm doing, I don't need tons of resources.. I'm not planning on writing 60 state Kalman filters or encoding 1080p video B. (the real kicker) I'd like to use processors available in DIP and QFP packages. I'd like the ability to prototype without soldering or baking anything in my toaster oven.

有什么想法吗?

推荐答案

在 C++ 中,您基本上只需为您使用的超出 C 可编译代码的内容付费,并且一些额外内容是免费的.

In C++ you essentially only pay for what you use over and above what would otherwise be C compilable code, and some of the extras are cost free.

一些针对小型目标的 C++ 编译器的最大问题是可用 C++ 实现的完整性或 C++ 编译器的可用性.

The biggest issue with some C++ compilers for small targets is the completeness of available C++ implementations or the availability of a C++ compiler at all.

EETimes/Embedded.com 多年来发表了许多关于该主题的文章:

EETimes/Embedded.com has run a number of articles on the subject over the years:

  • 即使在最低级别 - Dan Saks
  • 嵌入式 C++生成更快更小的代码 - John Carbone
  • 为什么 C++ 在嵌入式系统设计中是 C 的可行替代方案 - Fergus Bolger
  • 拒绝的不良理由C++ - 丹萨克斯
  • 在嵌入式应用程序中高效使用 C++ - Mentor Graphics/CesarA.奎罗斯
  • C++ 效率低下,事实还是虚构?- IAR Systems/Anders Lundgren

这些文章的大部分内容是,您不必在嵌入式系统中使用所有 C++,它们会根据内存、速度和各种特性的确定性来衡量或解释成本.您使用哪些部分取决于您的应用程序的性质(例如,它是否具有实时约束)以及目标平台的可用资源和性能.

The point most of these articles make is that you should not necessarily use all of C++ in an embedded system and they measure or explain the cost in terms of memory, speed, and determinism of various features. What parts you use will depend on the nature of your application (whether it has real-time constraints for example), and the available resources and performance of your target platform.

相关文章