简体   繁体   English

在面向对象的范例中使用C编程微处理器,是否可以建议?

[英]Programming a Microprocessor using C in Object Oriented Paradigm, is it Advisable?

Since C is commonly used in micro-controllers and it is possible to do object oriented programming in C, is it advisable to implement object oriented micro-controller programming using C? 由于C通常用于微控制器并且可以在C中进行面向对象编程,因此建议使用C实现面向对象的微控制器编程吗? What are the pros and cons? 优缺点都有什么?

My short answer is no. 我的简短回答是否定的。 Microcontrollers are highly restricted when it comes to program code memory, execution speed, and RAM. 在程序代码存储器,执行速度和RAM方面,微控制器受到严格限制。 Keeping things as simple as possible with C is advisable. 建议用C保持尽可能简单。 C is not meant to be an object-oriented language. C并不意味着是面向对象的语言。 The most you should consider doing is using pointers and structs, but don't try faking polymorphism or anything fancy like that. 你应该考虑做的最多的是使用指针和结构,但不要尝试伪造多态或类似的东西。

As long as you do not need polymorpism it is ok to pass structs around. 只要你不需要多态性,就可以传递结构。 But as soon as you use polymorphism/virtual function and putting functions inside these structs it might become very cryptic. 但是只要你使用多态/虚函数并将函数放在这些结构中,它就会变得非常神秘。

It also depends what you need to do. 这也取决于你需要做什么。 For drivers you do not need OO, maybe for application. 对于不需要OO的驾驶员,也许是申请。 Keep in mind that microcontrolers are low whit RAM, any you will need to keep low RAM footprint all the time in any case. 请记住,微控制器的RAM很低,任何情况下都需要始终保持低RAM占用空间。

If you do not plan to write more than 40k lines of application plain C is good enough and without fancy OO tricks. 如果你不打算写超过40k的应用程序,那么普通的C就足够了,而且没有花哨的OO技巧。

Yes, it's not only possible but in my opinion sometimes advisable. 是的,这不仅是可能的,而且在我看来有时是可取的。

On a small system, you need to be very aware of the costs of how you choose to do things. 在小型系统上,您需要非常了解选择执行操作的成本。 At the same time, there can be some real advantages of "lightweight" object orientation for organizing your code, particularly if you need to make a flexible toolkit for quickly implementing customizations, or even to permit runtime hotplugging. 同时,组织代码的“轻量级”面向对象可能具有一些真正的优势,特别是如果您需要制作灵活的工具包以快速实现自定义,甚至允许运行时热插拔。 A do-it-yourself lightweight object implementation (done for example with structs and function pointers) in C can be a good compromise. C中的自己动手轻量级对象实现(例如使用结构和函数指针)可以是一个很好的折衷方案。

Perhaps the best known example of this is the linux kernel. 也许最着名的例子是linux内核。

Yes, but if your tool-chain supports C++, you'd be better off using that. 是的,但如果您的工具链支持C ++,那么您最好使用它。 If the micro-controller is particularly resource constrained, or the application has hard real-time requirements, you will want to be fairly conservative in your use of C++, in particular the standard library, but you should use it nonetheless over C if the design and implementation are OO. 如果微控制器特别受资源限制,或者应用程序有严格的实时要求,那么在使用C ++时,尤其是标准库,您会希望相当保守,但如果设计,您应该使用C语言。并且实施是OO。

It's true, you can use OOP with C. You can also use #define to change keywords to look more like Python. 确实,您可以将OOP与C一起使用。您还可以使用#define将关键字更改为更像Python。 However, I would not suggest doing either. 但是,我不建议做任何一件事。

When I've seen someone try to do more complex OOP with C, it always ends up in unreadable code. 当我看到有人试图用C做更复杂的OOP时,它总是以不可读的代码结束。 When I see C code, I expect it to look like C, not someone's idea of how OOP in C should work. 当我看到C代码时,我希望它看起来像C,而不是某人想知道C中的OOP应该如何工作。

If you want OOP on a micro, use C++. 如果你想在微处理器上使用OOP,请使用C ++。 Many/most new micros support it. 许多/大多数新的微支持它。 Ignore those who say that micros don't have enough memory or speed because they have no idea how fast your micro is, how much memory it has, and what your performance constraints are. 忽略那些认为微观没有足够内存或速度的人,因为他们不知道你的微观有多快,它有多少内存,以及你的性能限制是什么。 Well written C++ will beat poorly written C in size and speed any day. 写得好的C ++会在任何一天的大小和速度上击败写得不好的C语言。

For my next embedded project, I will definitely be using parts of C++ and build an clean interface/class/static object based application with typedefs, defines and all the other nasty c left out. 我的下一个嵌入式项目,我肯定会使用 C ++ 零件和建立与类型定义,定义了一个干净的界面/班/静态的基于对象的应用程序和所有其他讨厌的C向左出来。 The things that I plan to utilize are: 我打算利用的东西是:

  • Classes . 课程 This allows me to encapsulate code and unit test with stub objects by means of configuration. 这允许我通过配置封装代码和单元测试与存根对象。
  • Interfaces . 接口 Gives me the power of a clear contract on each class compared to c header files which people tend to use as garbage cans for all kinds of typedefs, defines and stuff. 与c头文件相比,每个类都有明确的合同的强大功能,人们倾向于将其用作各种typedef,定义和东西的垃圾桶。 Also, interfaces gives me separation of definition and implementation and allows for unit testing with stub objects. 此外,接口使我可以分离定义和实现,并允许使用存根对象进行单元测试。
  • Static objects . 静态对象 I foresee no dynamic memory, so all objects will be static. 我预见没有动态内存,所以所有对象都是静态的。 Probably one application class will define and initialize everything and thus be the configuration of the application. 可能一个应用程序类将定义和初始化所有内容,因此是应用程序的配置。

All in all, it will compile into something that is as efficient as c, but with a much better overview. 总而言之,它将编译成与c一样高效的东西,但具有更好的概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM