在面向对象范式中使用 C 语言编程微处理器,是否可取?

发布于 2024-10-09 18:29:33 字数 66 浏览 9 评论 0原文

既然C在微控制器中常用,并且可以用C进行面向对象编程,那么使用C实现面向对象的微控制器编程是否可取?有什么优点和缺点?

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? What are the pros and cons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我的鱼塘能养鲲 2024-10-16 18:29:34

我的简短回答是否定的。微控制器在程序代码内存、执行速度和 RAM 方面受到严格限制。建议使用 C 语言使事情尽可能简单。 C 并不是一种面向对象的语言。您最应该考虑做的是使用指针和结构,但不要尝试伪造多态性或任何类似的花哨的东西。

My short answer is no. Microcontrollers are highly restricted when it comes to program code memory, execution speed, and RAM. Keeping things as simple as possible with C is advisable. C is not meant to be an object-oriented language. The most you should consider doing is using pointers and structs, but don't try faking polymorphism or anything fancy like that.

紫轩蝶泪 2024-10-16 18:29:34

只要不需要多态性,就可以传递结构。但是,一旦您使用多态性/虚拟函数并将函数放入这些结构中,它可能会变得非常神秘。

这也取决于您需要做什么。对于驱动程序,您不需要 OO,也许对于应用程序而言。请记住,微控制器的 RAM 占用率较低,因此您在任何情况下都需要始终保持较低的 RAM 占用空间。

如果您不打算编写超过 40k 行的应用程序,那么纯 C 就足够了,不需要花哨的 OO 技巧。

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. Keep in mind that microcontrolers are low whit RAM, any you will need to keep low RAM footprint all the time in any case.

If you do not plan to write more than 40k lines of application plain C is good enough and without fancy OO tricks.

梨涡 2024-10-16 18:29:34

是的,这不仅是可能的,而且在我看来有时是明智的。

在小型系统上,您需要非常清楚选择做事方式的成本。同时,“轻量级”面向对象在组织代码方面可能具有一些真正的优势,特别是如果您需要制作一个灵活的工具包来快速实现自定义,甚至允许运行时热插拔。在 C 中自己动手实现轻量级对象(例如使用结构和函数指针完成)可能是一个很好的折衷方案。

也许最著名的例子就是 Linux 内核。

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.

Perhaps the best known example of this is the linux kernel.

云淡风轻 2024-10-16 18:29:34

是的,但是如果您的工具链支持 C++,那么您最好使用它。如果微控制器的资源特别有限,或者应用程序具有严格的实时要求,那么您在使用 C++(特别是标准库)时会希望相当保守,但如果设计需要,您仍然应该使用它而不是 C。和实现都是OO的。

Yes, but if your tool-chain supports C++, you'd be better off using that. 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.

好听的两个字的网名 2024-10-16 18:29:34

确实如此,您可以将 OOP 与 C 一起使用。您还可以使用 #define 更改关键字,使其看起来更像 Python。但是,我不建议这样做。

当我看到有人尝试用 C 语言进行更复杂的 OOP 时,它总是以不可读的代码结束。当我看到 C 代码时,我希望它看起来像 C,而不是某人关于 C 中的 OOP 应该如何工作的想法。

如果您想在微型计算机上使用 OOP,请使用 C++。许多/大多数新的微型计算机都支持它。忽略那些说微控制器没有足够内存或速度的人,因为他们不知道您的微控制器有多快、有多少内存以及您的性能限制是什么。写得好的 C++ 随时都会在大小和速度上击败写得不好的 C。

It's true, you can use OOP with C. You can also use #define to change keywords to look more like 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. When I see C code, I expect it to look like C, not someone's idea of how OOP in C should work.

If you want OOP on a micro, use 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.

不打扰别人 2024-10-16 18:29:34

对于我的下一个嵌入式项目,我肯定会使用 C++ 的部分内容,并使用 typedef、定义和所有其他令人讨厌的 c 语言构建一个干净的基于接口/类/静态对象的应用程序。我计划使用的东西是:

  • 。这允许我通过配置的方式使用存根对象封装代码和单元测试。
  • 接口。与人们倾向于将 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. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文