在嵌入式环境中使用C ++ [英] Using C++ in an embedded environment

查看:150
本文介绍了在嵌入式环境中使用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天晚上,我和同事进行了一个非常有趣的对话,其中一个主题让我在今天晚上思考和搜索。在嵌入式环境中使用C ++(而不是C)。环顾四周,似乎有一些很好的交易,反对C ++提供的功能,但其他 Meyers 显然支持它。所以,我想知道谁能够清楚这个话题,以及社区的普遍共识。

解决方案

这取决于嵌入式系统的特殊性质和您使用的C ++的哪些功能。语言本身并不一定会生成比C更大的代码。



例如,如果内存是最严格的约束,那么您只需使用C ++C with classes - 也就是说,只使用直接成员函数,禁用RTTI,没有任何虚拟函数或模板。这将适合与等效的C代码相同的空间,因为您没有类型信息,vtables或冗余功能来混乱。



我发现模板是内存真的很紧时避免的最大的事情,因为每个模板功能的每个类型都有一个副本,这可以快速膨胀代码段。



在控制台视频游戏行业(嵌入式世界的最后端)C ++是王者。我们的限制是对内存(当前一代512MB)和实时性能的严格限制。通常使用虚拟函数和模板,但不是例外,因为它们会堆积堆栈并且成本太高。事实上,一个主要的制造商的编译器根本不支持例外。


Today I got into a very interesting conversation with a coworker, of which one subject got me thinking and googling this evening. Using C++ (as opposed to C) in an embedded environment. Looking around, there seems to be some good trades for and against the features C++ provides, but others Meyers clearly support it. So, I was wondering who would be able to shed some light on this topic and what the general consensus of the community was.

解决方案

It sort of depends on the particular nature of your embedded system and which features of C++ you use. The language itself doesn't necessarily generate bulkier code than C.

For example, if memory is your tightest constraint, you can just use C++ like "C with classes" -- that is, only using direct member functions, disabling RTTI, and not having any virtual functions or templates. That will fit in pretty much the same space as the equivalent C code, since you've no type information, vtables, or redundant functions to clutter things up.

I've found that templates are the biggest thing to avoid when memory is really tight, since you get one copy of each template function for each type it's specialized on, and that can rapidly bloat code segment.

In the console video games industry (which is sort of the beefy end of the embedded world) C++ is king. Our constraints are hard limits on memory (512mb on current generation) and realtime performance. Generally virtual functions and templates are used, but not exceptions, since they bloat the stack and are too perf-costly. In fact, one major manufacturer's compiler doesn't even support exceptions at all.

这篇关于在嵌入式环境中使用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆