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

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

问题描述

我一直在写一个库为我的项目(​​现在我使用的Arduino)。我的问题是该字符串 C ++和Arduino的在不同的。

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<串> 后来宣布字符串S; 。然而,在Arduino的字符串被定义的Arduino并宣布串S

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&LT;字符串方式&gt;

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的。通常,编程微控制器是不是很modulable,可能是非常不同的传统计算机程序。如果你真的想成为独立于任何微控制器和任何平台,你仍然可以使用C风格的字符串,用的char * 指向字符数组。

  2. 做你想要做的是不容易的。首先,你需要你的的std ::字符串库的源$ C ​​$ C。做一个的#include&LT;串GT; 是不够的。你还需要编译为AVR平台的字符串库

  3. 有一些限制的方式Arduino的过程中,C ++ code。与传统计算机程序相比的:例如,运营商删除未实现。此外,可以通过的std ::字符串的实施需要其他的东西,大量依赖管理,或code源尺寸过大的Arduino。

  1. I am not sure about your rational of doing something independent of Arduino. Usually, programming a microcontroller is not something very modulable and may be very different of a classical computer program. If you really want to be independent of any microcontroller 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-libc中与AVR-GCC来了,你可以看到这里的功能列表:
<一href=\"http://svn.savannah.nongnu.org/viewvc/%2acheckout%2a/trunk/avr-libc/include/string.h?root=avr-libc&content-type=text%2Fplain\">avr-libc string.h中
你甚至有美好的旧的printf :<一href=\"http://svn.savannah.nongnu.org/viewvc/%2acheckout%2a/trunk/avr-libc/include/stdio.h?root=avr-libc&content-type=text%2Fplain\">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

这是可能的,如果你加好头文件在code,但你的code的大小小心使用他们的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天全站免登陆