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.
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
.
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>
).
Is there a way to make Arduino use C++ string library, or convert string to Arduino string when compiling?
Several things:
char *
pointing to an array of chars.std::string
library. Doing an#include <string>
is not enough: you must also compile the string library for the AVR platform.new
anddelete
are not implemented. Moreover, other things may be needed by thestd::string
implementation, so lots of dependencies to manage, or a code source size too big for Arduino.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.hIt 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.To conclude, in my mind, the only portable way would be to use a
char *
string, http://arduino.cc/en/Reference/String