使用 C 或 C++ 的微控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8551164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microcontrollers using C or C++
提问by Ju-chan
Possible Duplicate:
Is there any reason to use C instead of C++ for embedded development?
I'm very curious about this: Why is it that when we deal with microcontrollers, they prefer C instead of C++? Based on my researches, C and Assembly language is the usual programming language for these devices. I only know C++ and Assembly Language. So in this case, should I start learning C or stick with Assembly language and if so, what compiler should I use because I only know the Turbo Assembler.
我对此很好奇:为什么当我们处理微控制器时,他们更喜欢 C 而不是 C++?根据我的研究,C 和汇编语言是这些设备的常用编程语言。我只知道 C++ 和汇编语言。所以在这种情况下,我应该开始学习 C 还是坚持使用汇编语言,如果是这样,我应该使用什么编译器,因为我只知道 Turbo Assembler。
Thanks and more power! :)
感谢和更多的力量!:)
采纳答案by Geoffroy
C is more low-level and does just exactly what you say. It is more adapted to low-resources environments such as micro-controllers.
C 更底层,完全按照你说的去做。更适合微控制器等资源匮乏的环境。
C++ as some features which requires additional resources (such as OOP, exception, and so on).
C++ 作为一些需要额外资源的特性(如 OOP、异常等)。
Moreover the micro-controller does not have the same features as your computer's CPU. It could for example not support dynamic library loading and even for static libraries you're limited in size as your chip doesn't have many memory.
此外,微控制器与您计算机的 CPU 具有不同的功能。例如,它可能不支持动态库加载,甚至对于静态库,由于您的芯片没有多少内存,您的大小也受到限制。
Usually, micro-controllers expose special input/output library, and the stdlib is not always available.
通常,微控制器公开特殊的输入/输出库,并且标准库并不总是可用。
What you need is a cross-compiler for your micro-controller specifically. Then you can write your program in C and ASM.
您需要的是专门用于微控制器的交叉编译器。然后你可以用 C 和 ASM 编写你的程序。
If the chip supports it, you can re-compile the stdlib to use the standard C features, and then you can eventually (once again if the chip has enough resources) build a C++ cross-compiler and then the STL. Then you will be able to build C++ program on your chip, but the program will weight much more than the original C program.
如果芯片支持它,您可以重新编译stdlib 以使用标准的C 特性,然后您最终可以(如果芯片有足够的资源再次)构建C++ 交叉编译器,然后是STL。然后您将能够在您的芯片上构建 C++ 程序,但该程序将比原始 C 程序重得多。
回答by David Brown
Some C++ features like exceptions and virtual functions can add overhead to your program which is undesirable in highly resource constrained environments. This reduces the demand for C++ compilers on such platforms. It is also much more difficult to implement a C++ compiler than a C compiler. This difficulty plus lack of demand makes it so many micro-controllers only have C compilers available for them.
某些 C++ 功能(如异常和虚函数)可能会给您的程序增加开销,这在资源高度受限的环境中是不可取的。这减少了此类平台上对 C++ 编译器的需求。实现 C++ 编译器也比实现 C 编译器困难得多。这种困难加上缺乏需求使得许多微控制器只有 C 编译器可用。
I would learn C for your micro-controller programming. It is not difficult to learn C after learning C++ and will be much easier to code in than assembly.
我会为您的微控制器编程学习 C。学完 C++ 再学 C 并不难,而且比汇编更容易编写代码。
回答by Martin James
It's just the availability of resources, really, as explained by the other posters. By the time you've compiled in a couple virtual method tables and a couple dozen object pointers, that's all the RAM gone from a simple uC!
正如其他海报所解释的那样,这只是资源的可用性。当您编译了几个虚拟方法表和几十个对象指针时,所有的 RAM 都从一个简单的 uC 中消失了!
That said, I prefer C++ on today's 32-bit controllers with 8K upwards of RAM, plenty of flash, complex embedded peripherals and multitasking libs. After decades of OO, using plain C is nightmarish for anything non-trivial.
就是说,我更喜欢在今天的 32 位控制器上使用 C++,它有 8K 以上的 RAM、大量的闪存、复杂的嵌入式外设和多任务库。经过几十年的面向对象,使用普通 C 对于任何非平凡的事情来说都是噩梦般的。
I currently use NXP ARM chips & Rowley Crossworks, (IDE, uses gcc). I only use C for lib interfaces and assembler for some drivers, all the rest is C++.
我目前使用 NXP ARM 芯片和 Rowley Crossworks,(IDE,使用 gcc)。我只将 C 用于 lib 接口和汇编器用于某些驱动程序,其余都是 C++。
回答by Pete Wilson
It is merely historical accident and practice (by old-time Luddites like me) that ucontrollers "prefer" ASM and C. If your compiler can compile C++ into ucontroller code, there's no theoretical reason that I know of why you should not use C++.
ucontroller“更喜欢”ASM和C只是历史事故和实践(像我这样的老路德分子)。如果您的编译器可以将C++编译为ucontroller代码,那么我就没有理论上的理由知道为什么您不应该使用C++。
To me, it's much easier and more natural to use ASM and C but you can use whichever you prefer so long as your compiler (and linker, if you use it) can do the right thing; and your ucontroller has enough memory to accomodate the (perhaps bigger) compiled C++ code.
对我来说,使用 ASM 和 C 更容易、更自然,但你可以使用任何你喜欢的,只要你的编译器(和链接器,如果你使用它)可以做正确的事情;并且您的 ucontroller 有足够的内存来容纳(可能更大)已编译的 C++ 代码。
回答by shr
Microcontrollers are memory and bandwidth constrained processing units. C programming language generates tight code that is close to assembly language in terms of size and speed. C++ usually carries an overhead in memory and speed. Another issue is dynamic memory allocation. Using object oriented design with C++ usually implies dynamically creating and destroying objects. Embedded applications using microcontrollers, typically allocate all the required memory statically and is not freed up for the life time of the application.
微控制器是内存和带宽受限的处理单元。C 编程语言生成的代码在大小和速度方面接近于汇编语言。C++ 通常会带来内存和速度方面的开销。另一个问题是动态内存分配。使用 C++ 的面向对象设计通常意味着动态创建和销毁对象。使用微控制器的嵌入式应用程序通常会静态分配所有所需的内存,并且不会在应用程序的整个生命周期内释放。
That being said, if you are using a 32 bit microcontroller and your application is complex enough that it handles either lot of data traffic or has significant user interface via touch screen / LCD etc., C++ (& sometimes even C# ) is the language of choice.
话虽如此,如果您使用的是 32 位微控制器,并且您的应用程序足够复杂,可以处理大量数据流量或通过触摸屏/LCD 等具有重要的用户界面,则 C++(有时甚至 C#)是选择。
The compiler that you choose would depend on the microcontroller, check the microcontroller vendor website for the appropriate development tool suite to use.
您选择的编译器取决于微控制器,请查看微控制器供应商网站以获取要使用的适当开发工具套件。
Assembly language is used only for the lowest layers if it cannot be done in C. It is harder to maintain and port assembly language code, hence it is best to minimize its use in your application.
如果不能在 C 中完成,汇编语言仅用于最低层。汇编语言代码更难维护和移植,因此最好尽量减少它在您的应用程序中的使用。
回答by neodelphi
Microcontrollers are small devices which are not very powerfull compared to computers. They have limited resources. Firstly, the size of the stack is very limited, thus it is not recommanded to have many nested function calls (on some devices, the stack is limited to a few bytes). Secondly, it is often not possible to dynamically allocate memory (alloc, free...), and most of the program data must be global static variables or stored in the stack, so usefull classes such as std::vector
would not be available.
微控制器是小型设备,与计算机相比功能不是很强大。他们的资源有限。首先,堆栈的大小是非常有限的,因此不建议有很多嵌套的函数调用(在某些设备上,堆栈被限制为几个字节)。其次,通常无法动态分配内存(alloc、free...),而且大部分程序数据必须是全局静态变量或存储在堆栈中,因此诸如此类有用的类std::vector
将不可用。
Even if C++ compilers could be used for microcontrollers, it would not be very useful since the low capabilities of these devices would forbid plain usage of this powerful language. Using C is often easier for simple tasks, and microcontrollers are sized for simple tasks.
即使 C++ 编译器可以用于微控制器,它也不是很有用,因为这些设备的低功能会禁止这种强大语言的简单使用。对于简单的任务,使用 C 通常更容易,并且微控制器的大小适合简单的任务。