如何结合 C++ 字符串和 Arduino 字符串? [英] How to combine C++ strings and Arduino Strings?

查看:46
本文介绍了如何结合 C++ 字符串和 Arduino 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的项目编写一个库(目前我正在使用 Arduino).我遇到的问题是 C++ 和 Arduino 中的 string 不同.

I have been writing a library for my project (for now I am using Arduino). The problem that I have is that string in C++ and in Arduino differ.

也就是说,我希望我的库独立于 Arduino,所以我使用了 #include ,然后声明了 string s;.但是在 Arduino 中,字符串由 Arduino 定义并声明为 String s2.

That is, I would like my library to be independent of Arduino, so I am using #include <string> and later declaring string s;. However in Arduino strings are defined by Arduino and declared String s2.

当我将我的库包含到草图中时,我在尝试包含 C++ 字符串库 (#include <string>) 的行上收到 error: string: No such file or directory).

When I include my library to the sketch I get error: string: No such file or directory on the line where I try to include C++ string library (#include <string>).

有没有办法让Arduino使用C++字符串库,或者在编译时将字符串转换为Arduino字符串?

Is there a way to make Arduino use C++ string library, or convert string to Arduino string when compiling?

推荐答案

几件事:

  1. 我不确定您独立于 Arduino 做某事的理由.通常,对微控制器进行编程并不是非常可模块化的,并且可能与经典的计算机程序非常不同.如果你真的想独立于任何微控制器和任何平台,你仍然可以使用 C 风格的字符串,其中 char * 指向一个字符数组.
  2. 做自己想做的事一点也不简单.首先,您需要 std::string 库的源代码.仅执行 #include 是不够的:您还必须为 AVR 平台编译字符串库.
  3. 与经典计算机程序相比,Arduino 处理 C++ 代码的方式存在一些限制":例如,未实现运算符 newdelete.此外,std::string 实现可能需要其他东西,因此需要管理大量依赖项,或者代码源对于 Arduino 来说太大了.
  1. I am not sure about your rationale of doing something independent of Arduino. Usually, programming a micro-controller is not something very modulable and may be very different of a classical computer program. If you really want to be independent of any micro-controller and any platform, you can still use C-style strings, with a char * pointing to an array of chars.
  2. Doing what you would like to do is not easy at all. First of all you would need the source code of your std::string library. Doing an #include <string> is not enough: you must also compile the string library for the AVR platform.
  3. There are some "limitations" in the way Arduino process the C++ code as compared with a classical computer program: for instance, the operators new and delete are not implemented. Moreover, other things may be needed by the std::string implementation, so lots of dependencies to manage, or a code source size too big for Arduino.

有一些用于 AVR 微控制器(如 Arduino)的经典字符串工具的实现,但它们是针对 C 样式字符串以程序方式(而不是以面向对象的方式)完成的.例如,对于 avr-gcc 附带的 avr-libc,您可以在此处查看函数列表:avr-libc string.h你甚至有很好的旧 printf:avr-libc stdio.h

There are some implementations of classical string tools for AVR microcontroller like Arduino, but they are done in a procedural way (and not in an object-oriented way) for C-style strings. For instance, for the avr-libc coming with avr-gcc, you can see the list of functions here: avr-libc string.h You even have the good old printf: avr-libc stdio.h

如果您在代码中添加好的头文件,则可以将它们与 Arduino 一起使用,但要注意代码的大小!仅使用 printf 就可以将大小增加几千字节,根据您的需要,这对于微控制器来说可能是巨大的.

It is possible to use them with Arduino if you add the good header files in your code, but beware of the size of your code! Just using printf can increase the size by several kilobytes, which can be huge for a microcontroller depending on your needs.

总而言之,在我看来,唯一可移植的方法是使用 char * 字符串,http://arduino.cc/en/Reference/String

To conclude, in my mind, the only portable way would be to use a char * string, http://arduino.cc/en/Reference/String

这篇关于如何结合 C++ 字符串和 Arduino 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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